Hexa Scholar

Floating point: why 0.1 + 0.2 isn't 0.3

A clay robot sets a gold ball into a notch on a long wooden rack whose notches sit at different spacings.
A value can only rest in one of the rack's notches, so the robot places it in the nearest one.

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. 1 SeeToggle bits to approach a target.
  2. 2 TryPredict, then adjust the precision.
  3. 3 ProveBuild a bound for rounding error.
  4. 4 ApplySolve five numerical problems.
Before you start · 3 quick checks on powers, distance, and fractions

Two ideas before you start

2 ideas

Binary 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.

1 See

Build the closest number

Short ticks are available values. The dashed vertical line is the target; the moving triangle is your value.
2 Try

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.

3 Prove

Bound the error of one rounding

Theorem

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.

4 Apply

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.

Key point Identify the precision, exponent, and rounding rule before calculating. Separate a guaranteed error bound from an actual error, and choose a tolerance that answers the question in its stated units.
Level IntroPrerequisites powers and fractions, refreshed aboveTime ~30 min

Further reading