Foundations to Frontier
Tests
A. Mathematical Foundations › A8 Numerical Methods and Scientific Computing

Floating Point: IEEE 754, Precision and Rounding Error

Computers store real numbers as floating-point approximations: a sign, an exponent and a fixed number of significant bits. You will learn how IEEE 754 doubles and singles are laid out, what machine epsilon and ulps mean, why 0.1 + 0.2 is not 0.3, how rounding errors propagate, and how catastrophic cancellation and absorption destroy accuracy. You will fix them with stable reformulations and compensated (Kahan) summation, and analyse the Patriot missile clock failure and float32 timestamp drift in robot software.

Level 0: Foundation

★☆☆☆☆ Introductory⏱ 5 minGeneral audience. No mathematics required.

Computers round numbers

A calculator showing 8 digits can’t write ⅓ exactly; it shows 0.33333333 and drops the rest. Computers do the same thing, in binary. Most decimals, even simple ones like 0.1, can’t be written exactly in binary, so the computer stores the nearest number it can. The tiny differences are usually harmless, but they can pile up or suddenly explode if you’re not careful.

What a computer actually stores
float32: 0 01111011 10011001100110011001101
stored: 0.100000001490116119385 · error 1.49e-9 · ulp 7.45e-9
float64: 0 01111111011 1001100110011001100110011001100110011001100110011010
stored: 0.100000000000000005551 · ulp 1.39e-17
sign · exponent · fraction
fp16fp32fp64log₁₀|x| →log ulp
at |x| = 1.0e+0: gap to next float is 9.8e-4 (fp16, max 65504) · 1.2e-7 (fp32) · 2.2e-16 (fp64)
A float is ±1.fraction × 2^exponent. 0.1 has no finite binary expansion, so it is rounded; 16777217 = 2²⁴ + 1 cannot be stored in float32 at all. The gap between neighbouring floats grows in proportion to |x|. Relative precision stays fixed (2⁻²⁴ for fp32), but adding a small number to a large one can lose it entirely.
Why engineers care

In 1991 a Patriot missile battery failed to intercept a Scud because its clock, counting tenths of a second in a 24-bit register, had drifted by a third of a second after 100 hours. In 1996, Ariane 5 flight 501 was lost when a 64-bit float was converted to a 16-bit integer that overflowed. Numerical details are flight-critical.

📝 Quick check (Level 0)
0/3 answered
  1. 1. Can 0.1 be stored exactly as a binary floating-point number?
  2. 2. In most languages, 0.1 + 0.2 == 0.3 is…
  3. 3. Tiny rounding errors can…

Level 1: Beginner

★★☆☆☆ Basic⏱ 20 minSecondary-school mathematics: arithmetic and elementary algebra.

The format

A normal IEEE 754 float stores x=(−1)s×1.f×2e−biasx = (-1)^s\times1.f\times2^{e - \text{bias}}.

Format Bits Exponent Fraction Machine ε Max
fp16 (half) 16 5 10 2⁻¹⁰ ≈ 9.8e−4 65504
fp32 (single) 32 8 23 2⁻²³ ≈ 1.2e−7 3.4e38
fp64 (double) 64 11 52 2⁻⁵² ≈ 2.2e−16 1.8e308

Machine epsilon ε is the gap between 1 and the next float. The ulp (unit in the last place) of xx is the gap at xx: about ε∣x∣\varepsilon\lvert x\rvert. Round-to-nearest guarantees

fl⁡(x)=x(1+δ),∣δ∣≤u=ε/2\operatorname{fl}(x) = x(1 + \delta),\quad\lvert\delta\rvert\le u = \varepsilon/2

Special values

Specials include ±0, ±∞ (overflow, 1/0), NaN (0/0, √−1), and subnormals: tiny numbers below 2−10222^{-1022} that fill the gap to zero with reduced precision.

📝 Quick check (Level 1)
0/3 answered
  1. 1. fp32 machine epsilon ≈
  2. 2. The ulp of x grows…
  3. 3. 0/0 produces…

Level 2: Intermediate

★★★☆☆ Intermediate⏱ 40 minUndergraduate students and practising engineers.

How errors arise

  • Representation error: 0.1 is stored as 0.1000000000000000055511…
  • Absorption: 1016+1=101610^{16} + 1 = 10^{16} in double, because 1 is below half an ulp.
  • Catastrophic cancellation: subtracting nearly equal numbers leaves only their rounding noise. 1−cos⁡(10−8)1 - \cos(10^{-8}) evaluates to 0, while the identity 2sin⁡2(x/2)2\sin^2(x/2) gives 5×10−175\times10^{-17} accurately.
  • Accumulation: a naive sum of nn terms has error bound ≈(n−1)u∑∣xi∣\approx(n - 1)u\sum\lvert x_i\rvert.

Fixes

  • Rewrite formulas to avoid cancellation: log1p, expm1, the stable quadratic formula, and the identity above.
  • Kahan compensated summation carries a correction term. Its error bound, 2u∑∣xi∣2u\sum\lvert x_i\rvert, doesn’t depend on nn.
  • Pairwise summation, which NumPy uses, has error O(ulog⁡n)O(u\log n).
  • Count integer ticks instead of adding a float time step repeatedly.
🐍 Exercise 1: dissecting doubles
Decode a double into sign, exponent and significand. Find machine epsilon. Demonstrate representation error, absorption and catastrophic cancellation. Compare naive and Kahan summation of a million copies of 0.1.
📝 Quick check (Level 2)
0/3 answered
  1. 1. 1 − cos(x) for tiny x suffers from…
  2. 2. Kahan summation's error bound depends on n…
  3. 3. For small x, log(1 + x) should be computed with…

Level 3: Advanced

★★★★☆ Advanced⏱ 40 minGraduate level, with full derivations and proofs.

The standard model of rounding

For each basic operation ∘∈{+,−,×,÷}\circ\in\{+, -, \times, \div\}, IEEE 754 guarantees correct rounding: fl⁡(a∘b)=(a∘b)(1+δ)\operatorname{fl}(a\circ b) = (a\circ b)(1 + \delta) with ∣δ∣≤u\lvert\delta\rvert\le u. From this, error bounds follow by induction.

Summation error bound

Let sk=fl⁡(sk−1+xk)s_k = \operatorname{fl}(s_{k-1} + x_k). Each step multiplies previous errors by (1+δk)(1 + \delta_k). So

∣s^n−sn∣≤γn−1∑∣xi∣,γk=ku1−ku\lvert\hat s_n - s_n\rvert\le\gamma_{n-1}\sum\lvert x_i\rvert,\quad\gamma_k = \frac{ku}{1 - ku}

∎ (Higham, 2002, §4.2.)

Condition number of subtraction

The relative condition number of f(a,b)=a−bf(a, b) = a - b is ∣a∣+∣b∣∣a−b∣\frac{\lvert a\rvert + \lvert b\rvert}{\lvert a - b\rvert}, which is huge when a≈ba\approx b. Input errors of relative size uu become output errors of size u⋅κu\cdot\kappa. That’s cancellation: it doesn’t create errors, it exposes them.

Exercise 3.1: digits lost in 1.000001 − 1.000000 (in decimal arithmetic)

κ=2.000001/10−6≈2×106\kappa = 2.000001/10^{-6}\approx2\times10^6, so about 6 significant digits are lost.

📝 Quick check (Level 3)
0/3 answered
  1. 1. Correct rounding guarantees fl(a + b) = (a + b)(1 + δ) with |δ| ≤ …
  2. 2. Condition number of a − b when a ≈ b is…
  3. 3. The naive summation error bound grows like…

Level 4: Expert

★★★★★ Expert⏱ 35 minResearchers: original papers and open problems.

Clock drift: the Patriot failure and float timestamps

The Patriot counted time in tenths of a second and converted to seconds by multiplying by 0.1, chopped to 24 bits. Each tick lost ≈9.5×10−8\approx9.5\times10^{-8} s. After 100 hours of continuous operation the clock was off by 0.34 s. A Scud travelling at 1676 m/s moves about 575 m in that time, enough to shift the radar’s range gate off the target (GAO, 1992). The same bug appears today when robot software accumulates t += dt in float32. After 200 s of 1 kHz IMU samples, the sum is off by almost half a second. The robust fix is to count integer ticks and compute t=n Δtt = n\,\Delta t.

🐍 Exercise 2 (research): Patriot clock drift and timestamp accumulation
Reproduce the Patriot chopping error and its 100-hour drift and tracking error. Then accumulate 1 kHz timestamps in float32 and float64, and compare with integer tick counting.

Floating point in AI, robotics and aerospace

  • Flight software: Ariane 501 (1996) was lost to an unprotected float-to-int16 conversion. MISRA-C and JPL coding standards restrict float comparisons and conversions.
  • State estimation: covariance matrices lose symmetry and positive-definiteness in single precision. Joseph-form and square-root Kalman filters fix this (A2 and A9).
  • Deep learning: fp16, bf16 and fp8 training needs loss scaling and higher-precision accumulators (next topic).
  • Long-duration navigation: absolute times are stored as integer seconds plus a fraction, or as two doubles (e.g. JPL SPICE ephemeris time and GPS week/seconds).

References

  1. IEEE (2019). IEEE Standard for Floating-Point Arithmetic, IEEE 754-2019.
  2. D. Goldberg (1991). “What every computer scientist should know about floating-point arithmetic.” ACM Computing Surveys, 23(1).
  3. N. Higham (2002). Accuracy and Stability of Numerical Algorithms, 2nd ed. SIAM. Ch. 1–4.
  4. W. Kahan (1965). “Further remarks on reducing truncation errors.” Communications of the ACM, 8(1).
  5. U.S. GAO (1992). Patriot Missile Defense: Software Problem Led to System Failure at Dhahran, Saudi Arabia. GAO/IMTEC-92-26.
  6. J.-L. Lions et al. (1996). Ariane 5 Flight 501 Failure: Report by the Inquiry Board. ESA.
  7. J.-M. Muller et al. (2018). Handbook of Floating-Point Arithmetic, 2nd ed. Birkhäuser.
  8. L. Trefethen, D. Bau (1997). Numerical Linear Algebra. SIAM. Lectures 12–15.