IEEE 754: floating-point numbers from the inside#
“Floating point computation is by nature inexact, and programmers can easily misuse it so that the computed answers consist almost entirely of ‘noise.’” — Donald E. Knuth, The Art of Computer Programming, vol. 2, §4.2.2
Every float in Python, every double in C, almost every floating-point number on a graphics card
is a number in a format of the IEEE 754 standard. In Lecture 1
(§1.4)
we met the binary64 format and the first surprises: 0.1 + 0.2 != 0.3,
(1e16 + 1) - 1e16 == 0. This chapter goes further, in two parts:
Part 1. The standard: what IEEE 754 promises — what the standard promises: a single rule of correct rounding, the format and the distribution of the numbers on the real line, rounding modes, signed zero, infinities and NaN, gradual underflow, Sterbenz’s lemma.
Part 2. Implementation: tricks and subtleties — what it looks like in practice: in the bits, in the compiler, in Python, and in libraries. Tricks (the bits as a logarithm, the fast inverse square root, the exact error of addition, FMA) and subtleties (double rounding, NaN in lists, \(-0.0\) in printed output, elementary functions).
Materials
Applet: Anatomy of a floating-point number — the 64 bits in memory, the density of machine numbers, a comparison with exact arithmetic.
Lecture 1, §1.4 — the binary64 format, \(\varepsilon=2^{-52}\), compensated summation in
sum, Rump’s polynomial.Lab 3 — computer exercises for both parts, and exercises for independent practice.
Map of examples#
Example |
What it shows |
Where |
|---|---|---|
200,000 random operations and zero errors |
the correct-rounding rule can be tested |
|
almost half of the nonnegative |
the bit layout: order of the numbers = order of the integers |
§2 |
\(10^{22}\) yes, \(10^{23}\) no |
which integers are machine numbers |
§2 |
\(1.00\to1.01\to1.02\to\dots\) |
why ties are broken to even |
§3 |
\(3\cdot(4/3-1)-1=-2^{-52}\) |
the machine gives away its own precision |
§3 |
\(\sqrt{-1+0i}=i\), but \(\sqrt{-1-0i}=-i\) |
what signed zero is for |
§4 |
\(x\ne y\Rightarrow x-y\ne0\) |
subnormal numbers and gradual underflow |
§4 |
\((1+10^{-15})-1\) |
subtracting nearby numbers is exact |
§5 |
|
the bits as an integer, distance in ulps |
|
|
the bits are almost a logarithm: the fast inverse square root |
§1 |
the error of an addition is a machine number |
TwoSum, TwoProduct, compensated summation |
§2 |
\(\sqrt{a\cdot a-a\cdot a}=\mathrm{NaN}\) in C |
FMA and a compiler that fuses operations |
§3 |
same program, different results |
double rounding, |
§4 |
median of the same list: \(\mathrm{nan}\), \(1.5\), or \(2.5\) |
NaN in Python lists |
§5 |
\(\sin(10^{15}+0.3)\) |
the function computes correctly, but not for the intended argument |
§6 |
\((1+1/n)^n\to1\) |
formulas robust to rounding: |
§7 |
Muller’s sequence tends to \(100\) instead of \(6\) |
rounding picks a different limit |
§7 |
How to read this chapter#
Lecture 1 and basic Python are enough. You need modules of the standard library (
math,cmath,fractions,decimal,struct,statistics), NumPy, mpmath, and Matplotlib, and two cells of Part 2 compile short C programs (clang).Exact arithmetic is provided by
fractions.Fraction. The conversionfloat(Fraction(...))rounds correctly, so it is our reference, against which we compare the machine’s results.All results are computed when the page is built. A few depend on the computer (on the system’s math library or on the C compiler), and one depends on the Python version; all such places are pointed out in the text.