Section 4.49

Binary floating-point representation, special values, and rounding

Definition

Binary floating-point is a way to store an approximation of a real number in a fixed number of bits: a sign, an exponent and a significand, a number with a fixed count of binary digits. The value is plus or minus the significand times 2 to the power of the exponent. The IEEE 754 standard defines the common formats, binary32 (C’s usual float) and binary64 (double), reserves bit patterns for zeros, infinities and NaN (not a number), and requires each arithmetic result to be rounded by a stated rule [271].

The idea

A 32-bit signed integer covers about ±2.1×109\pm 2.1 \times 10^{9} in steps of 1. Science and engineering need both 6.02×10236.02 \times 10^{23} and 1.6×10−191.6 \times 10^{-19}. Floating-point uses scientific notation in base 2: a fixed number of significant binary digits, and an exponent that moves the binary point. Every value then carries about the same relative precision, and the gaps between neighboring values grow with magnitude: they double at each power of two.

The set of representable values is finite, so almost every real number falls between two of them and must be rounded. Even 0.1 cannot be stored exactly. A fraction has a finite binary expansion only if its denominator, in lowest terms, is a power of two, and 1/101/10 has the factor 5 in its denominator. The binary64 value nearest to 0.1 is exactly 0.1000000000000000055511151231257827021181583404541015625, and 0.1+0.20.1 + 0.2 evaluates to 0.30000000000000004 in binary64. Rounding is part of the definition of the arithmetic, not a malfunction.

A ruler whose marks double their spacing at each power of two is the picture: fine near zero, coarse far away, with as many marks between 1 and 2 as between 1024 and 2048. The picture stops at the special values. Signed zeros, infinities and NaN are not marks on the ruler.

How it works

Fields. A binary32 value has 32 bits in three fields, starting from the most significant bit:

The binary64 format has the same structure with 1, 11 and 52 bits.

Classes. The exponent field decides how to read the other two fields.

  1. 1≤e≤2541 \le e \le 254: a normal number, with value (−1)s×(1+f/223)×2e−127(-1)^s \times (1 + f/2^{23}) \times 2^{e - 127}. The exponent is stored with a bias: the true exponent is e−127e - 127, so fields from 1 to 254 give exponents from −126-126 to 127127 without a separate sign. The leading 1 of the significand is not stored, because every normal significand starts with 1; this hidden bit gives 24 significant bits from 23 stored ones.

  2. e=0e = 0 and f=0f = 0: zero, +0+0 or −0-0 according to the sign.

  3. e=0e = 0 and f≠0f \ne 0: a subnormal number, with value (−1)s×(f/223)×2−126(-1)^s \times (f/2^{23}) \times 2^{-126}. The leading bit is 0 and the scale stays at the smallest normal exponent, so subnormals fill the gap between the smallest normal number and zero with evenly spaced values.

  4. e=255e = 255 and f=0f = 0: +∞+\infty or −∞-\infty.

  5. e=255e = 255 and f≠0f \ne 0: NaN, the result of an operation that has no meaningful value, such as 0/00/0 or ∞−∞\infty - \infty. The standard recommends, and most processors follow, that a NaN whose highest fraction bit is 1 is quiet: it passes silently through later operations. A signaling NaN, with that bit 0, raises the invalid-operation exception when an operation uses it.

The binary64 format uses bias 1023 and exponent fields from 1 to 2046 for normal numbers, and has 53 significant bits.

Rounding. IEEE 754 requires addition, subtraction, multiplication, division, square root and fused multiply-add (a×b+ca \times b + c with a single rounding) to be correctly rounded: the result is the exact mathematical result, rounded once to a representable value. The default rule is round to nearest, ties to even: take the nearest representable value, and on an exact tie take the one whose last significand bit is 0. The other rules round toward zero, toward +∞+\infty and toward −∞-\infty.

Spacing. For a normal binary32 number xx with 2E≤|x|<2E+12^E \le |x| < 2^{E+1}, neighboring representable values are 2E−232^{E - 23} apart. This gap is the ULP (unit in the last place) of xx: the value of the last significand bit. Round to nearest is never wrong by more than half an ULP.

Exceptions. IEEE 754 names five exceptional events: invalid operation (for example 0/00/0, which gives NaN), division by zero (1/01/0 gives +∞+\infty), overflow (a result too large for the format, which becomes ±∞\pm\infty under round to nearest but the largest finite value under round toward zero), underflow (a result too small to be normal and not exact) and inexact (the result was rounded). By default each event sets a status flag and the operation returns its default result; the program does not stop.

Comparison. NaN is unordered: every comparison <<, ≤\le, ==, ≥\ge, >> that involves a NaN is false, and ≠\ne is true, even when a NaN is compared with itself. The two zeros compare equal, yet 1/(+0)=+∞1/(+0) = +\infty and 1/(−0)=−∞1/(-0) = -\infty.

Worked example

Encoding 1.5. In binary, 1.5 is 1.121.1_2, that is 1+1/21 + 1/2, times 202^0. So s=0s = 0. The true exponent 0 is stored as e=0+127=127e = 0 + 127 = 127, binary 01111111. The bits after the leading 1 are a 1 followed by 22 zeros, so f=222=4,194,304f = 2^{22} = 4{,}194{,}304. Check: 1+4,194,304/8,388,608=1.51 + 4{,}194{,}304 / 8{,}388{,}608 = 1.5, and 2127−127=12^{127 - 127} = 1. The 32 bits are 0 01111111 10000000000000000000000; grouped in fours, 0011 1111 1100 0000 followed by sixteen zeros, they read 0x3FC00000. Byte order §4.21 changes only how those four bytes are laid out in memory: 00 00 C0 3F on a little-endian machine.

Extreme values of binary32. The largest finite value has e=254e = 254 and every fraction bit 1: (2−2−23)×2127≈3.4028235×1038(2 - 2^{-23}) \times 2^{127} \approx 3.4028235 \times 10^{38}. The smallest positive normal value has e=1e = 1 and f=0f = 0: 2−126≈1.1754944×10−382^{-126} \approx 1.1754944 \times 10^{-38}. The smallest positive subnormal value has e=0e = 0 and f=1f = 1: 2−23×2−126=2−149≈1.4012985×10−452^{-23} \times 2^{-126} = 2^{-149} \approx 1.4012985 \times 10^{-45}. Subnormals are 2−1492^{-149} apart, and 2232^{23} such steps reach 223×2−149=2−1262^{23} \times 2^{-149} = 2^{-126}, exactly the smallest normal value, so the spacing continues without a jump.

Rounding an integer. In binary32 every integer up to 224=16,777,2162^{24} = 16{,}777{,}216 is exact. From 2242^{24} to 2252^{25} the ULP is 224−23=22^{24 - 23} = 2, so only even integers exist there. The integer 16,777,217 lies exactly halfway between 16,777,216 and 16,777,218. Both are even integers, so evenness of the integer cannot break the tie. The rule looks at the last significand bit instead: 16,777,216 is 1.00…002×2241.00\ldots00_2 \times 2^{24}, with last bit 0, and 16,777,218 is 1.00…012×2241.00\ldots01_2 \times 2^{24}, with last bit 1. The result is 16,777,216. By the same rule, 16,777,219 rounds to 16,777,220, whose last bit is 0. A binary32 counter that adds 1 at a time therefore stops at 16,777,216.

Order of operations. In binary64 the ULP from 2532^{53} to 2542^{54} is 2. Take a=253a = 2^{53}, b=1b = 1 and c=−253c = -2^{53}. For (a+b)+c(a + b) + c: the exact sum 253+12^{53} + 1 is a tie between 2532^{53} and 253+22^{53} + 2; ties to even gives 2532^{53}, and adding cc gives 0. For a+(b+c)a + (b + c): 1−253=−9,007,199,254,740,9911 - 2^{53} = -9{,}007{,}199{,}254{,}740{,}991 is exact, because its magnitude is below 2532^{53}, and adding aa gives 1. Real addition is associative; rounded addition is not.

Stated precisely

For binary32, with ss, ee and ff the field values defined above, the value vv of a bit pattern is v={(−1)s×(1+f⋅2−23)×2e−127if 1≤e≤254,(−1)s×f⋅2−23×2−126if e=0 (a signed zero when f=0),(−1)s×∞if e=255 and f=0,NaNif e=255 and f≠0.v = \begin{cases} (-1)^s \times (1 + f \cdot 2^{-23}) \times 2^{e - 127} & \text{if } 1 \le e \le 254, \\ (-1)^s \times f \cdot 2^{-23} \times 2^{-126} & \text{if } e = 0 \text{ (a signed zero when } f = 0\text{)}, \\ (-1)^s \times \infty & \text{if } e = 255 \text{ and } f = 0, \\ \text{NaN} & \text{if } e = 255 \text{ and } f \ne 0. \end{cases} For binary64, replace 23 by 52, 127 by 1023, 254 by 2046, −126-126 by −1022-1022 and 255 by 2047.

Let pp be the number of significant bits (24 for binary32, 53 for binary64) and u=2−pu = 2^{-p} the unit roundoff. Every integer nn with |n|≤2p|n| \le 2^p is representable. If xx and yy are representable, ∘\circ is one of ++, −-, ×\times, ÷\div, the rule is round to nearest, and the exact result x∘yx \circ y lies in the normal range, then the computed result is fl(x∘y)=(x∘y)(1+δ),|δ|≤u,\mathrm{fl}(x \circ y) = (x \circ y)(1 + \delta), \qquad |\delta| \le u, where fl\mathrm{fl} denotes the rounded floating-point operation and δ\delta is the relative rounding error. For binary32, u=2−24≈5.96×10−8u = 2^{-24} \approx 5.96 \times 10^{-8}; for binary64, u=2−53≈1.11×10−16u = 2^{-53} \approx 1.11 \times 10^{-16}. Machine epsilon, the gap between 1 and the next larger representable value, is 2u2u: 2−232^{-23} and 2−522^{-52}.

The bound rests on these assumptions: the hardware or library rounds correctly; the result neither overflows nor underflows; the rounding rule is round to nearest; and the compiler does not change the operation, for example by computing in a wider format, by reordering operations, or by fusing a multiplication and an addition into one rounded step.

Limits and common mistakes

Decimal fractions are approximations. Testing a computed value with == fails for this reason: 0.1+0.2=0.30.1 + 0.2 = 0.3 is false in binary64. Money and other decimal quantities belong in integers that count the smallest unit, such as cents, or in a decimal type.

A range check written with ordinary comparisons lets NaN through. The test if (x < lo || x > hi) reject(); rejects nothing when xx is NaN, because both comparisons are false. Write the test so that NaN fails it, as in if (!(x >= lo && x <= hi)) reject();, or test isfinite(x) first.

Integers lose exactness above 2242^{24} in binary32 and above 2532^{53} in binary64. An identifier such as 9,007,199,254,740,993 read into a binary64 number becomes 9,007,199,254,740,992. JSON §12.26 (JavaScript Object Notation) parsers in JavaScript, whose ordinary number type is binary64, do exactly this.

In C, converting a NaN, an infinity, or a value outside the target type’s range to an integer type is undefined behavior §4.12.

Hardware and compiler settings can change the arithmetic. Some processors offer a flush-to-zero mode that replaces subnormal results with zero for speed, and on some processors subnormal arithmetic is much slower than normal arithmetic, which makes timing depend on the data. Compiler options §12.3 such as -ffast-math let the compiler assume that no NaNs or infinities occur and reorder operations.

Printing is a separate conversion. A value printed with few digits may hide the difference between two stored values, and printing more digits shows the stored binary value, never the decimal text that was originally parsed.

Questions answered

Does every 32-bit pattern mean a finite real number?

No. In the IEEE 754 binary32 format, the 32 bits are a sign bit, an 8-bit exponent field and a 23-bit fraction field, and the exponent field value 255 (all ones) is reserved for values that are not finite. With a zero fraction it means +∞+\infty or −∞-\infty; with any nonzero fraction it means NaN (not a number). Exponent field 255 occurs in 2×223=16,777,2162 \times 2^{23} = 16{,}777{,}216 patterns, of which 2 are infinities and 16,777,21416{,}777{,}214 are NaNs. The other 232−224=4,278,190,0802^{32} - 2^{24} = 4{,}278{,}190{,}080 patterns are finite: normal numbers, subnormal numbers (exponent field 0 and a nonzero fraction), and two zeros, +0+0 and −0-0, which are different patterns for the same real number. Code that reads four bytes as a binary32 value must therefore check that the value is finite, for example with isfinite, before treating it as a quantity.

Why can normal binary32 have 24 significant bits with only 23 fraction bits stored?

Because the leading bit of every normal significand is 1, the format does not store it. A normal binary32 number is (−1)s×1.f2×2e−127(-1)^s \times 1.f_2 \times 2^{e - 127}, where ss is the sign bit, ee the 8-bit exponent field, and 1.f21.f_2 means a binary 1 followed by the 23 stored fraction bits ff after the binary point. The exponent is always chosen so that the first significant bit sits just before the binary point, which makes that bit 1 for every normal number; storing it would waste a bit. For example, 1.5 is 1.12×201.1_2 \times 2^0: the stored fraction is a 1 followed by 22 zeros, and the hidden 1 is added back when the value is read. Subnormal numbers, which have exponent field 0, have a hidden bit of 0 instead, so they have fewer significant bits, down to one for the smallest.

Why does ties-to-even mean a retained bit rule rather than an even decimal integer?

Because even refers to the last significand bit that the rounded result keeps, not to whether the number is an even integer. When an exact result lies exactly halfway between two representable values, round to nearest, ties to even picks the one whose last significand bit is 0. The two candidates are often both even integers, so integer evenness could not decide. In binary32, from 224=16,777,2162^{24} = 16{,}777{,}216 to 2252^{25} only even integers are representable. The integer 16,777,217 is halfway between 16,777,216 and 16,777,218, which are both even. The first is 1.00…002×2241.00\ldots00_2 \times 2^{24}, with last significand bit 0; the second is 1.00…012×2241.00\ldots01_2 \times 2^{24}, with last bit 1. The rule picks 16,777,216. Deciding by the last bit sends ties up and down about equally often, so rounding errors do not drift in one direction over many operations.

Can more printed digits or a repeated sample recover precision already lost in parsing?

No. Parsing decimal text into a binary floating-point number rounds it to the nearest representable value, and every later step sees only that stored value. Different texts can round to the same value: in binary64, 9007199254740993 and 9007199254740992 both become 253=9,007,199,254,740,9922^{53} = 9{,}007{,}199{,}254{,}740{,}992. Printing more digits shows the stored binary value more exactly, never the original text. Repeating the parse gives the same value every time, because rounding is deterministic, and the average of identical values is that same value. Averaging many different noisy measurements can reduce random measurement error, but that is a statement about noise in the measurements, not a way to undo rounding. To keep the exact input, keep the text, or use a representation that holds it exactly, such as an integer or a decimal type.

Is a NaN check interchangeable with checking a finite permitted range?

No. A NaN (not a number) check rejects only NaN, so +∞+\infty, −∞-\infty and finite values outside the permitted range still pass it. A range check that rejects when x < lo || x > hi is true catches out-of-range finite values and both infinities, but lets NaN through, because every ordered comparison with a NaN is false. Each test alone is incomplete. A single test that rejects all three cases rejects when !(x >= lo && x <= hi) is true: that expression is true for NaN, for both infinities and for finite values outside the range. Where it matters, first classify the value with isfinite, then check the range, and decide separately what the program does with each class.

Related entries: Signed versus unsigned comparison (§2.12), Endianness (§4.21), JSON (§12.26), Counter quantization (§11.35). Further reading: [187, 190, 191, 271].