Floating point: why 0.1 + 0.2 isn't 0.3
The question
How close can a handful of binary digits get to 0.1, and what happens when those stored numbers are added?
Build the number before inspecting the computer's answer.
- 1 SeeToggle bits to approach a target.
- 2 TryPredict, then adjust the precision.
- 3 ProveBuild a bound for rounding error.
- 4 ApplySolve five numerical problems.
Before you start · 3 quick checks on powers, distance, and fractions
Two ideas before you start
2 ideasBinary places are halves, quarters, and eighths
A bar shows the contributions of the selected binary digits.
Each place contributes its weight when its bit is on. Select a pattern to see which pieces make its value.
Exact definition
A bit is a digit that is either zero or one. The subscript in \((1.b_1b_2\ldots)_2\) means base two. The leading digit contributes one; each later place has half the weight of the previous place.
\[ (1.b_1\ldots b_{p-1})_2 = 1+\sum_{j=1}^{p-1}b_j2^{-j}. \]The significand is this leading digit and its fractional digits. Its precision \(p\) counts all significant binary digits, including the leading one.
An exponent sets the scale
A fixed binary pattern moves along a number line as its exponent changes.
The exponent multiplies the whole significand by a power of two. The binary point can move while the number of significant digits stays fixed.
Exact definition
A positive normal binary floating-point value has the form
\[ x=(1.b_1\ldots b_{p-1})_2\,2^e, \]where \(e\) is an integer. A sign bit extends this to negative values. Zero needs a separate representation. For a fixed exponent, the significand is at least one and less than two. This groups values into intervals between consecutive powers of two.
The toy format and the computer format
The bit game uses a short significand so every choice is visible. Its arithmetic rounds to the nearest available value. At a tie, it chooses the value whose integer significand is even, meaning its last retained bit is zero. Each operation rounds separately.
The toy arithmetic allows every integer exponent and treats zero exactly. Real hardware has a limited exponent range. Near zero, subnormal values use a different spacing rule; very large results can overflow. The normal-number bounds below exclude those cases. The separate computer example uses JavaScript's binary64 arithmetic.
Build the closest number
- Toggle the four fractional bits. The leading bit and the exponent stay fixed.
- Watch your value and its signed error, then press Check my bits to compare all available choices.
Now change the scale
Both rows below use the same precision and the same horizontal units. The lower row uses the next larger exponent. Move the exponent and compare neighboring ticks. A row's endpoint is also the first value in the next interval; the endpoint is included to show the last gap.
Why a finite binary expansion cannot equal a tenth
Any finite binary fraction can be written as an integer divided by a power of two. If that fraction equaled a tenth, cross multiplication would make a power of two divisible by five. No power of two has that factor. More bits can improve the approximation, but no finite bit string ends the expansion.
A reduced fraction terminates in binary exactly when its denominator is a power of two. Decimal termination uses a different rule because its base also contains the prime factor five. Whether a short written number is exact in memory depends on the base, not on how familiar the number looks.
Add on a short grid
The next question uses the toy format, with rounding after the addition. The result and the precision controls appear after you commit a choice.
Find enough precision
Use what you found above. Each goal asks for the smallest precision that keeps an increment when it is added to one. Changing the precision computes a new result from the original inputs; it does not repair an already rounded value.
Trace the computer example
| Quantity | Exact value |
|---|
Printing a decimal is another operation
A short printed decimal often identifies a binary value without displaying its complete decimal expansion. Formatting a result to fewer decimal places can make outputs look equal. It does not change the stored values, and strict equality still compares those stored values.
For an approximate calculation, choose an error test from the problem's units and acceptable error. An absolute tolerance controls a difference in those units. A relative tolerance compares the difference with the size of the quantities. Near zero, a relative tolerance alone can be too restrictive. Machine epsilon is the gap above one in a format. It is not a universal tolerance for a whole algorithm.
Bound the error of one rounding
Fix an integer precision \(p\ge2\) and an integer exponent \(e\). Consider the positive normal grid on \([2^e,2^{e+1}]\), including the upper endpoint. Assume both endpoints are representable and there is no overflow or underflow. Let \(\operatorname{fl}(x)\) round to the nearest grid value, using ties to even.
Write \(\Delta\) for the gap between adjacent grid values. For every real \(x\) in this interval,
\[\begin{aligned} \Delta &=2^{e-p+1},\\ |\operatorname{fl}(x)-x|&\le\Delta/2,\\ \frac{|\operatorname{fl}(x)-x|}{x}&\le2^{-p}. \end{aligned}\]The last inequality is a uniform upper bound. It need not be the exact largest relative error on the interval.
Choose the spacing, examine a midpoint, then use the nearest endpoint. Each wrong option below is false for every precision and exponent in the theorem.
One operation versus a whole calculation
The theorem bounds one rounding relative to its exact input. For an addition, that input is the exact sum of the already stored operands. Errors already present in the operands are a separate source. Subtracting nearly equal inputs can leave a small difference whose relative error is large, even if the subtraction itself rounds accurately.
Likewise, the Taylor remainder bounds the mathematical approximation error. It does not by itself bound the roundoff produced while evaluating the polynomial. The lesson on the limit defining e uses a careful logarithm calculation because a tiny increment can be lost when added directly to one.
A limit in convergent sequences concerns exact real numbers. Evaluating more terms in a fixed format need not reproduce every improvement of the exact sequence. A numerical stopping rule should use an error estimate and the available precision together.
Put the grid to work
Give a decimal or a fraction. The first four problems require exact answers. The last states its tolerance. Each problem has hints and a worked solution.
Further reading
- Oracle, Numerical Computation Guide, “Double Format”, for the binary64 fields and normal values.
- Ecma International, ECMAScript Language Specification, “The Number Type”, for JavaScript's rounding rule.