Home › Arithmetic › Lesson 10

Floating-Point Arithmetic and IEEE 754

Lesson 10 of 12

Key idea of this section

A binary floating-point number is ±(1.f)₂ × 2ᵉ with a p-bit significand.
Every real x in the normal range rounds to fl(x) = x(1 + δ) with |δ| ≤ u = 2⁻ᵖ.
IEEE 754 requires +, −, ×, ÷ and √ to be correctly rounded:
fl(x ∘ y) = (x ∘ y)(1 + δ),  |δ| ≤ u.

Floating-point systems

Exact real arithmetic is impossible on a finite machine, and exact rational arithmetic is too expensive for most scientific computation because denominators grow without bound. The practical compromise is scientific notation with a fixed number of digits.

Definition 10.1. The floating-point system F(β, p, emin, emax) consists of 0 and the numbers ±(d₀.d₁d₂…d₍p−1₎)β × βᵉ with digits 0 ≤ dᵢ < β, d₀ ≠ 0 and emin ≤ e ≤ emax. The integer p is the precision. The machine epsilon ε = β1−p is the gap between 1 and the next larger element, and the unit roundoff is u = ½β1−p.

The elements are not uniformly spaced. In each binade [βᵉ, βᵉ⁺¹) there are (β − 1)βp−1 elements with constant spacing βe−p+1, the unit in the last place, ulp. Relative spacing is therefore roughly constant, between ε/β and ε, which is exactly what scientific computation needs.

The IEEE 754 standard

Before 1985 every manufacturer had its own floating-point arithmetic, with different precisions, rounding rules and exceptional behaviour, and portable numerical software was nearly impossible. The IEEE 754 standard (1985), whose principal architect was William Kahan (Turing Award 1989), fixed binary formats and their semantics; revisions in 2008 and 2019 added decimal formats, the fused multiply-add and recommended elementary functions. The three most common binary formats are

format      sign  exponent  fraction   p    bias   emin   emax   machine ε
binary16      1       5        10      11     15     −14      15    2⁻¹⁰ ≈ 9.77 × 10⁻⁴
binary32      1       8        23      24    127    −126     127    2⁻²³ ≈ 1.19 × 10⁻⁷
binary64      1      11        52      53   1023   −1022    1023    2⁻⁵² ≈ 2.22 × 10⁻¹⁶

A word with sign bit s, biased exponent field E (w bits) and fraction field f is decoded as follows.

Worked Example 10.2 — encoding and decoding in binary32. To encode −6.25: 6.25 = 110.01₂ = 1.1001₂ × 2², so s = 1, E = 2 + 127 = 129 = 10000001₂ and f = 1001 followed by 19 zeros. The word is 1 10000001 10010000000000000000000, or C0C80000 in hexadecimal. Conversely, the word 40490FDB has s = 0, E = 10000000₂ = 128 and f = 490FDB₁₆, which decodes to 1.5707963705… × 2¹ = 3.14159274101257…, the binary32 number nearest to π.

Key numbers for binary64 (the type double in C and the type of every JavaScript number): the largest finite value is (2 − 2⁻⁵²) × 2¹⁰²³ ≈ 1.7977 × 10³⁰⁸; the smallest positive normal value is 2⁻¹⁰²² ≈ 2.2251 × 10⁻³⁰⁸; the smallest positive subnormal is 2⁻¹⁰⁷⁴ ≈ 4.94 × 10⁻³²⁴; and every integer of absolute value at most 2⁵³ = 9007199254740992 is exactly representable, but 2⁵³ + 1 is not. For binary32 the corresponding numbers are about 3.4028 × 10³⁸, 1.1755 × 10⁻³⁸, 1.4013 × 10⁻⁴⁵ and 2²⁴ = 16777216.

Rounding

The default rounding mode is round to nearest, ties to even: a real x is rounded to the nearest floating-point number, and in case of a tie to the one whose last significand bit is 0. Three directed modes (toward 0, toward +∞, toward −∞) are also required; the latter two make interval arithmetic possible.

Theorem 10.3 (Standard model). If x ∈ ℝ lies in the normal range of a binary format with precision p and fl(x) denotes x rounded to nearest, then fl(x) = x(1 + δ) with |δ| ≤ u = 2⁻ᵖ. IEEE 754 requires that for x, y in the format and ∘ ∈ {+, −, ×, ÷}, the computed result is fl(x ∘ y), and likewise for √.

Proof. Suppose 2ᵉ ≤ |x| < 2ᵉ⁺¹. Floating-point numbers in this binade are spaced 2e−p+1 apart, and the endpoint 2ᵉ⁺¹ is also representable, so |fl(x) − x| ≤ ½ · 2e−p+1 = 2e−p. Hence |fl(x) − x|/|x| ≤ 2e−p/2ᵉ = 2⁻ᵖ. ∎

For binary64, u = 2⁻⁵³ ≈ 1.11 × 10⁻¹⁶: each operation is accurate to about 16 significant decimal digits. The guarantee concerns a single operation. It says nothing directly about a sequence of operations, which is the subject of Lesson 11.

The decimal fraction 0.1

Since 1/10 has a factor 5 in the denominator, its binary expansion 0.0(0011)₂ does not terminate (Theorem 8.3), and fl(0.1) is a different number. In binary64 the stored values are exactly

fl(0.1)         = 0.1000000000000000055511151231257827021181583404541015625
fl(0.2)         = 0.200000000000000011102230246251565404236316680908203125
fl(0.3)         = 0.299999999999999988897769753748434595763683319091796875
fl(0.1) ⊕ fl(0.2) = 0.3000000000000000444089209850062616169452667236328125

where ⊕ denotes floating-point addition. The sum is the floating-point number immediately above fl(0.3), so the comparison 0.1 + 0.2 == 0.3 is false in every IEEE 754 binary64 environment, and the shortest decimal string that reads back to the computed sum is 0.30000000000000004. Every one of these numbers is a dyadic rational with denominator dividing 2¹⁰⁷⁴, and every one has a terminating decimal expansion, because 2 divides 10.

The same phenomenon had real consequences. During the Gulf War, in February 1991, a Patriot missile battery at Dhahran failed to intercept an incoming Scud missile. Its clock counted tenths of a second in an integer and multiplied by 1/10 stored in a 24-bit fixed-point register; the chopping error of about 9.5 × 10⁻⁸ per tenth accumulated over 100 hours of continuous operation (3.6 × 10⁶ tenths) to about 0.34 s, enough for the tracking gate to miss a target moving at about 1,700 m/s. Twenty-eight soldiers were killed.

Failure of the field axioms

Floating-point addition is commutative but not associative. In binary64,

(2⁵³ ⊕ 1) ⊕ 1 = 2⁵³ ⊕ 1 = 2⁵³,      but      2⁵³ ⊕ (1 ⊕ 1) = 2⁵³ + 2

because 2⁵³ + 1 lies exactly halfway between the representable numbers 2⁵³ and 2⁵³ + 2, and ties go to the even significand, which is 2⁵³. Distributivity fails as well, and x ⊕ y = x can hold with y ≠ 0. Multiplication by a power of 2 is exact (barring overflow and underflow), and so are the operations whose exact result is representable, a fact exploited in Lesson 11.

Subnormals and special values

Without subnormal numbers, the gap between 0 and the smallest normal number 2emin would be much larger than the gap just above it. With them (gradual underflow, championed by Kahan), the implication x ≠ y ⇒ x ⊖ y ≠ 0 holds for all finite floating-point x, y, a property that protects code such as "if x ≠ y then z = 1/(x − y)". Infinities and signed zeros make 1/(+0) = +∞ and 1/(−0) = −∞, and NaN is the result of invalid operations such as 0/0, ∞ − ∞ and √−1; NaN compares unequal to everything, including itself, so x ≠ x is the standard test for NaN.

Exercise

  • (a)
    Decode the binary32 word 0 10000010 01100000000000000000000.
  • (b)
    How many binary64 numbers lie in [1, 2), and what is the next binary64 number after 1?
  • (c)
    Encode 0.15625 in binary32, giving the three fields.
Show solution

Answers: (a) E = 130, so the exponent is 130 − 127 = 3, and the significand is 1.011₂ = 1.375; the value is 1.375 × 8 = 11. (b) The exponent is fixed at 0 and the 52 fraction bits are free, so there are 2⁵² numbers; the successor of 1 is 1 + 2⁻⁵² = 1 + ε ≈ 1.0000000000000002. (c) 0.15625 = 5/32 = 0.00101₂ = 1.01₂ × 2⁻³, so s = 0, E = −3 + 127 = 124 = 01111100₂, and f = 01 followed by 21 zeros; the word is 3E200000 in hexadecimal.

V.3.1