The float64 type (in Python: float, in C: double) stores a number in 64 bits: the sign, the exponent and the fractional part of the significand. There are only finitely many such numbers, so almost every real number — even one as innocent as 0.1 — has to be rounded. Below we compute exactly (with integers of arbitrary length) what was stored and how large the error is. Every bit can be clicked.

Number or expression

Examples

Allowed: numbers (also 1e-3), + - * / ^ (or **), parentheses, NaN, Infinity. The expression is evaluated twice: in float64 (every operation rounded) and exactly, with fractions.

64 bits in memory

Click any bit to flip it — you will see which number the new bit pattern represents. Hexadecimal:

What was entered, what was stored

Exact value (what we wanted)
Value stored in float64 — exactly
How Python/JS prints it (shortest representation)
Error: stored − exact
Relative error

In red: the digits from which the stored value differs from the exact value.

float64 versus exact arithmetic

Computationfloat64 (hardware)Exactagree?

Rump’s example at different precisions

$f(a,b) = 333.75\,b^6 + a^2(11a^2b^2 - b^6 - 121b^4 - 2) + 5.5\,b^8 + \frac{a}{2b}$ for $a = 77617$, $b = 33096$. We evaluate the same formula, rounding the result of every operation to $p$ significand bits (in software, with “round to nearest, ties to even”).

Significand bits pEquivalentResultcorrect?

Density of machine numbers — a toy system

Numbers of the form $\pm(1.b_1\dots b_{p-1})_2\cdot 2^{e}$. Every interval $[2^e, 2^{e+1})$ contains the same number of them, so the gap between neighbors doubles at every power of two. float64 works the same way, only with $p = 53$ and $e \in [-1022, 1023]$.

What is going on here?

A float64 number with fields $s$ (1 bit), $e$ (11 bits) and $f$ (52 bits) represents

$$x = (-1)^s \cdot \Bigl(1 + \frac{f}{2^{52}}\Bigr)\cdot 2^{\,e-1023} \qquad (1 \le e \le 2046),$$

and for $e = 0$ — the subnormal number $(-1)^s\cdot \frac{f}{2^{52}}\cdot 2^{-1022}$ (including $\pm 0$); $e = 2047$ encodes $\pm\infty$ and NaN. Every finite machine number therefore has the form $M\cdot 2^{E}$ with integers $M, E$ — it has a finite binary and decimal expansion. On the other hand, $0.1 = \frac{1}{10}$ has an infinite binary expansion ($0.0\overline{0011}_2$), because $10$ is divisible by $5$. That is why the stored value is the nearest machine number: $\frac{3602879701896397}{2^{55}}$.

Rounding “to nearest” guarantees (except for overflow and the subnormal range) that $$\mathrm{fl}(x) = x(1+\delta),\qquad |\delta| \le u = 2^{-53} \approx 1.11\cdot 10^{-16}.$$ The same holds for every single operation $+,-,\times,\div$ (the IEEE 754 standard requires correct rounding). The errors of successive operations can, however, accumulate and — when nearby numbers are subtracted — grow dramatically (so-called cancellation). Rump’s example shows something even worse: the results with 100, 113 and 120 bits agree with one another and are all wrong. Agreement between computations at increasing precision is therefore no proof of correctness.

The takeaway for the whole course: floating-point arithmetic is fast and usually good enough, but a numerical result is an approximation with an error. If we need certainty, we compute exactly (fractions, integers of arbitrary length — as this applet and computer algebra systems do) or with guaranteed error bounds (interval arithmetic).

Explore on your own

  1. Which of the numbers $0.5$, $0.25$, $0.2$, $0.75$, $0.1$ are stored exactly in float64? State a criterion for a fraction $\frac{a}{b}$ in lowest terms.
  2. Enter 2^53 and then 2^53+1. Why is the result the same? What is the gap between neighboring machine numbers in the interval $[2^{53}, 2^{54})$?
  3. Flip the least significant bit of the significand of the number 1. By how much does the value change? And for the number 1024? Compare with the “gap” fields in the side panel.
  4. Find the smallest positive machine number (5e-324) and the largest finite one. What happens to the relative accuracy in the subnormal range?