# Floating Point

Floating point represents small and large values with similar relative accuracies in a fixed amount of space.

It’s much like scientific notation, with a fixed number number of significant digits, multiplied by a power of the number base.

## 1 Representable Values

$$+0, -0, +\infty, -\infty$$, and lots of NaN.

Normal numbers of the form $\pm M \times 2^{E - B - (P-1)}, \quad M \in [2^{P-1}, 2^P) \subset \mathbb{N}, \quad E \in[1, 2^R - 1) \subset \mathbb{N}.$

Denormalized numbers of the form $\pm M \times 2^{1-B - (P-1)}, \quad M \in [1, 2^{P-1})\subset \mathbb{N}.$

Parameters:

• $$2$$: base (usually $$2$$, sometimes $$10$$, rarely anything else)

• $$P$$: precision (number of mantissa bits including implicit leading $$1$$)

• $$R$$: range (number of bits for exponent)

• $$B$$: bias (offset for exponent, usually midway: $$B = 2^{R-1}-1$$).

## 2 Standard Types

Name Size Precision Range Denormal Smallest Largest
half 16 11 5 6.0e-8 6.10e-5 65504.0
single 32 24 8 1.0e-45 1.18e-38 3.40e38
double 64 53 11 5.0e-324 2.23e-308 1.80e308
x87 long double 80 64 15 4.0e-4951 3.36e-4932 1.19e4932
quad double 128 113 15 6.0e-4966 3.36e-4932 1.19e4932

## 3 Data Format

Sign bit.

• clear: positive
• set: negative

Exponent bits.

• all clear: zero (mantissa zero) or denormalized (mantissa nonzero)
• all set: infinity (mantissa zero) or NaN (mantissa nonzero)
• otherwise: normalized number

Mantissa bits.

• leading 1 is implicit for normalized numbers
• leading 1 is explicit for denormalized numbers
• negative numbers are not complemented (just change sign bit)

Note: x87 long double is different.

## 4 long double

In the C programming language, long double is implementation dependent.

The language standard says only that it has at least as much precision as double. Some platforms have long double as an alias for double (MSVC, armv7). Others use the x87 format (i386, x86_64/amd64). The x87 format may be padded to 96 bits or 128 bits, depending on compiler options and architecture. Some other platforms alias long double to _Float128 which is another name for quad double (aarch64).
{"ft_lang_label":"__label__en","ft_lang_prob":0.6341136,"math_prob":0.9918205,"size":2138,"snap":"2023-40-2023-50","text_gpt3_token_len":725,"char_repetition_ratio":0.10824742,"word_repetition_ratio":0.024024025,"special_character_ratio":0.36155286,"punctuation_ratio":0.1448598,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99776334,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-04T06:47:19Z\",\"WARC-Record-ID\":\"<urn:uuid:ecf246f2-1140-403b-a17a-ce267a4e88f2>\",\"Content-Length\":\"7493\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:d5bf3b02-9177-4bea-b2a5-1492d35017ae>\",\"WARC-Concurrent-To\":\"<urn:uuid:cbe292b7-301d-46a8-b414-16d0845f6de8>\",\"WARC-IP-Address\":\"193.170.194.196\",\"WARC-Target-URI\":\"https://mathr.co.uk/web/floating-point.html\",\"WARC-Payload-Digest\":\"sha1:QWRYLR6AYQW7AFQ47JXB4X2DES3D4H3I\",\"WARC-Block-Digest\":\"sha1:OSSYY2ZRQKOPZIOVZMXWJVHFYDK3P3UC\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100525.55_warc_CC-MAIN-20231204052342-20231204082342-00286.warc.gz\"}"}