A floating-point number is scientific notation in base 2 with a fixed number of digits. Because most fractions do not end in binary (see Binary Fractions), nearly every real number has to be rounded to fit, and the pattern of that rounding explains a lot of computer arithmetic.
A (normalized) float with $p$ fraction bits is $$x = \pm(1.b_1b_2\ldots b_p)_2 \times 2^{e},$$ where the exponent $e$ lies in a fixed range. A double (IEEE 754 double precision) uses 1 sign bit, 11 exponent bits and $p = 52$ fraction bits: 64 bits in all, with 53 significant bits counting the leading 1, which is not stored.
Key idea
Why it works
Worked example: Floats in a toy system
- In $[1, 2)$: $(1.b_1b_2b_3)_2$ for the 8 choices of bits: $1, 1.125, 1.25, \ldots, 1.875$, a gap of $2^{-3} = 0.125$.
- In $[4, 8)$ the exponent is $2$, so the gap is $2^{-3} \times 2^{2} = 0.5$: the floats there are $4, 4.5, 5, \ldots, 7.5$.
- Machine epsilon for this system is $2^{-3} = 0.125$.
Answer: gap $0.125$ in $[1, 2)$, gap $0.5$ in $[4, 8)$
Worked example: Rounding and its error
- $2.7$ is in $[2, 4)$, where the gap is $2^{-3} \times 2 = 0.25$: the neighbors are $2.5$ and $2.75$.
- $2.75$ is nearer: $\mathrm{fl}(2.7) = 2.75$, an error of $0.05$.
- Relative error: $\frac{0.05}{2.7} \approx 0.0185$, below $u = \frac{\varepsilon}{2} = 0.0625$.
Answer: $2.75$, relative error about $0.0185$
Worked example: Why 0.1 + 0.2 is not 0.3
- $0.1$ is stored as $0.100\,000\,000\,000\,000\,005\,55\ldots$ and $0.2$ as $0.200\,000\,000\,000\,000\,011\,1\ldots$: each is rounded.
- Their exact sum, $0.300\,000\,000\,000\,000\,016\,6\ldots$, is rounded again, to the double $0.300\,000\,000\,000\,000\,044\,4\ldots$
- The double nearest $0.3$ is $0.299\,999\,999\,999\,999\,988\,9\ldots$, one gap lower. So the test "0.1 + 0.2 == 0.3" is false.
- Compare floats with a tolerance instead: $|a - b| \le 10^{-12}\max(|a|, |b|)$, say.
Answer: Each value is rounded to the nearest double, and the roundings do not cancel.
| Double precision | Value |
|---|---|
| fraction bits $p$ | $52$ |
| machine epsilon $\varepsilon = 2^{-52}$ | $\approx 2.22 \times 10^{-16}$ |
| unit roundoff $u = 2^{-53}$ | $\approx 1.11 \times 10^{-16}$ |
| largest double | $\approx 1.8 \times 10^{308}$ |
| smallest normal double | $\approx 2.2 \times 10^{-308}$ |
Common mistake