Lesson

Back to the Question: Rounding and Machine Epsilon

The stored values behind the opening question, a tie resolved to even, machine epsilon, and how the gap between numbers grows with their size.

Last updated:


In this lecture we learn to:

  • answer “why is 0.1 + 0.2 not 0.3” with exact numbers: stored values, an exact sum and a tie;
  • bound the rounding error of a stored number and say what machine epsilon tells you about it;
  • explain why the gaps between stored numbers grow with their size, and when adding a small number changes nothing.

What we take from “How Computers Store Numbers”

The lecture How Computers Store Numbers built the format. Three takeaways from it are needed here:

  • a normal number is stored as a sign, an exponent with a bias and a fraction, and it stands for (−1)s×2e×1.f(-1)^{s}\times2^{e}\times1.f⁠;
  • binary64 has one sign cell, 11 exponent cells and 52 fraction cells;
  • the decimal 0.10.1 has a binary expansion that never ends, so it is not stored exactly.

Back to the question: why 0.1+0.2≠0.30.1+0.2\neq0.3

In one sentence: 0.1 and 0.2 are both stored a little too large, and 0.3 is stored a little too small.
The exact sum of the two stored numbers lands exactly halfway between two adjacent stored numbers, and the tie is broken upwards.
The result is the stored number that comes right after the stored 0.3, so the two numbers differ.

We can now answer the opening question with exact numbers. Every normal binary64 number (one with an unstored leading 1, that is, not zero, subnormal, infinity or NaN) can be written as a whole number of equal small units, where the unit is the value of the last fraction bit.
This unit is constant between two consecutive powers of 2 and doubles at every power of 2, so larger numbers lie farther apart.
We measure the errors in units of u=2−56u=2^{-56}⁠, the unit of numbers between 116\tfrac{1}{16} and 18\tfrac{1}{8}⁠, such as 0.1.

Predict: the bits of 0.1 after place 52 are 1001 1001…1001\,1001\ldots Will 0.1 be rounded up or down?

  1. Storing 0.1. From the lecture How Computers Store Numbers, in binary 0.1=0.00011‾2=0.0001 1001 1001…20.1=0.0\overline{0011}_2=0.0001\,1001\,1001\ldots_2 never ends, so it is rounded to 53 significant bits.
    This is how we find the stored value:

    • Move the point four places to get a 1 before it: 0.1=2−4×1.1001 1001 1001…20.1=2^{-4}\times1.1001\,1001\,1001\ldots_2⁠.

    • There are 52 fraction bits, that is, 13 groups of four. The first twelve groups are 10011001⁠, and the 13th group is 10011001 followed by 1001 1001…1001\,1001\ldots after place 52.

    • What is left after the 52nd bit is 0.1001 1001…2=0.60.1001\,1001\ldots_2=0.6 of the last bit’s unit, u=2−56u=2^{-56}⁠:
      more than half: the next bit is 1 and another 1 follows, so we round up.
      The last group 10011001 becomes 10101010⁠.

    • Rounding added a whole unit in place of the 0.60.6 that was cut, that is, 1−0.6=0.41-0.6=0.4 of uu⁠:

      fl⁡(0.1)=0.1+0.4 u≈0.1+5.55×10−18\operatorname{fl}(0.1)=0.1+0.4\,u\approx0.1+5.55\times10^{-18}

      The stored value is slightly larger than 0.1. On the page Computing the stored representation: two methods it is also computed in another way.

    Predict: 0.2=2×0.10.2=2\times0.1⁠. By how many uu does the stored 0.2 differ from 0.2?

  1. Storing 0.2. Doubling changes only the exponent, so fl⁡(0.2)=2fl⁡(0.1)\operatorname{fl}(0.2)=2\operatorname{fl}(0.1)⁠. Now the last place is 2u2u and rounding added 0.40.4 of it:

    fl⁡(0.2)=0.2+0.8 u\operatorname{fl}(0.2)=0.2+0.8\,u
  2. Adding the two stored numbers, exactly.

    fl⁡(0.1)+fl⁡(0.2)=0.3+0.4 u+0.8 u=0.3+1.2 u\operatorname{fl}(0.1)+\operatorname{fl}(0.2)=0.3+0.4\,u+0.8\,u=0.3+1.2\,u

    This exact sum has to be stored, so it is rounded to a nearby stored number.
    We need to know where the stored numbers near 0.3 are.

    Predict: the bits cut off from 0.3 are 0011 0011…0011\,0011\ldots More than half of the last place, or less?
    Which way will 0.3 be rounded?

  1. Storing 0.3. In binary 0.3=0.010011‾2=2−2×1.0011 0011…20.3=0.01\overline{0011}_2=2^{-2}\times1.0011\,0011\ldots_2⁠. The 13 groups 00110011 are the 52 fraction bits, and the cut-off part is 0.0011 0011…2=315=0.20.0011\,0011\ldots_2=\tfrac{3}{15}=0.2 of the last place, less than half, so we round down.
    The last place here is 2−54=4u2^{-54}=4u⁠, so:

    fl⁡(0.3)=0.3−0.2⋅4u=0.3−0.8 u\operatorname{fl}(0.3)=0.3-0.2\cdot4u=0.3-0.8\,u

    The stored neighbour above it is one place higher: fl⁡(0.3)+4u=0.3+3.2 u\operatorname{fl}(0.3)+4u=0.3+3.2\,u⁠.

    Predict: the exact sum is 0.3+1.2 u0.3+1.2\,u⁠, and the two stored numbers around it are 0.3−0.8 u0.3-0.8\,u and 0.3+3.2 u0.3+3.2\,u⁠.
    Which of them is it closer to?

  1. A tie. The exact sum 0.3+1.2 u0.3+1.2\,u is 2u2u above fl⁡(0.3)\operatorname{fl}(0.3) and also 2u2u below the neighbour above it, that is, exactly halfway.
    The IEEE 754 rule (round to nearest, ties to even) picks the neighbour whose last fraction bit is 0.
    The fraction of fl⁡(0.3)\operatorname{fl}(0.3) ends in 00110011 (last bit 1) and that of the neighbour above ends in 01000100 (last bit 0), so the tie goes up:

    fl⁡(0.1)+fl⁡(0.2) → 0.3+3.2 u=fl⁡(0.3)+4u\operatorname{fl}(0.1)+\operatorname{fl}(0.2)\ \to\ 0.3+3.2\,u=\operatorname{fl}(0.3)+4u

The computed sum and the stored 0.3 are neighbours, one gap of 2−54≈5.6×10−172^{-54}\approx5.6\times10^{-17} apart.
So 0.1 + 0.2 == 0.3 is False, and Python prints the sum as 0.30000000000000004.
Nothing malfunctioned: each step followed the rules exactly.
Compare 0.5 + 0.25 == 0.75: all three numbers are stored exactly and the sum fits, so no rounding happens at all.

Python can show what is stored for 0.1, that is, the first step.
The method .hex() writes a float in hexadecimal (base 16), and Decimal(x) from the standard decimal module converts a float to a decimal number without rounding, so it reveals the exact stored value:

from decimal import Decimal
print((0.1).hex())
print(Decimal(0.1))
0x1.999999999999ap-4
0.1000000000000000055511151231257827021181583404541015625

The first line says 1.999999999999a16×2−41.999999999999\text{a}_{16}\times2^{-4}⁠. Each hexadecimal digit is four bits, so the 13 digits after the point are the 52 fraction bits.
Each group 10011001 of the first step is the hexadecimal digit 9, and the last group 10101010 is a:
that is the rounding up. The second line is the stored value we found, 0.1000000000000000055511…0.1000000000000000055511\ldots⁠.

The next explorer shows the three fields at their real lengths.
Choose binary64 and jump to 0.1: the fraction digits are 10011001 again and again, and at the end the last group is 10101010 instead of 10011001⁠.
That is the rounding up we found in the first step. Flip the last fraction bit and see how much the value changes.
When the number is 1, the row “Gap to the next number here” is the machine epsilon of the chosen format:
2−102^{-10} in binary16, 2−232^{-23} in binary32 and 2−522^{-52} in binary64.

The interactive explorer needs JavaScript.

Check the arithmetic yourself; Fraction(x) (from the fractions module) is the exact value of the stored number x:

from fractions import Fraction
# the exact sum of the two stored numbers
exact_sum = Fraction(0.1) + Fraction(0.2)
# exactly half a gap above the stored 0.3
print(exact_sum - Fraction(0.3) == Fraction(1, 2**55))
print(0.1 + 0.2 == 0.30000000000000004)
True
True

Rounding and machine epsilon: how large can the error be?

A note on rounding. When a number lies between two stored numbers, IEEE 754 stores the nearer one.
In the rare case of an exact tie it chooses the one whose last fraction bit is 0, for example …0100\ldots0100 and not …0011\ldots0011 (“round to nearest, ties to even”).
We write fl(x)\mathrm{fl}(x) for the stored number nearest to xx (fl stands for floating point, not for floor).
So the absolute error of storing a normal number xx is at most half the gap gg to its neighbour, and gg is at most εmach ∣x∣\varepsilon_{\text{mach}}\,|x|⁠:

∣fl(x)−x∣ ≤ g2 ≤ εmach2 ∣x∣|\mathrm{fl}(x)-x|\ \le\ \frac{g}{2}\ \le\ \frac{\varepsilon_{\text{mach}}}{2}\,|x|

Dividing by ∣x∣|x| gives the relative error:

∣fl(x)−xx∣≤εmach2=2−53≈1.1×10−16(binary64),\begin{aligned} \left|\frac{\mathrm{fl}(x)-x}{x}\right|&\le\frac{\varepsilon_{\text{mach}}}{2}\\ &=2^{-53}\approx1.1\times10^{-16}\quad\text{(binary64)}, \end{aligned}

Some books call 2−532^{-53} itself the machine epsilon; in these notes, as in Python’s sys.float_info.epsilon, it is the gap between 1 and the next number.

1. A fixed relative accuracy, in every size. One rounding changes a normal binary64 number by a relative amount of at most 2−53≈1.1×10−162^{-53}\approx1.1\times10^{-16}⁠.
This is below 5×10−165\times10^{-16}⁠, the bound for rounding to 16 significant decimal digits, whatever the size of the number.
This is a statement about relative error, not about digits after the decimal point.

2. Large numbers lose small additions. Near 101610^{16} the gap between stored numbers is 2, so adding 1 changes nothing:

print(1e16 + 1 == 1e16)   # True
True

The exact sum 1016+110^{16}+1 lies exactly halfway between two stored numbers, and the tie goes to the one whose last fraction bit is 0, which is 101610^{16}⁠.
More generally, a number smaller than half the gap beside a larger number disappears when it is added to it:
we say it is absorbed.

Choose a format, and zoom in or out to see all the numbers that the format stores, and how dense they are in different parts of the number line:

The explorer needs JavaScript.

Explore how the gap changes with the size of the number, and where adding 1 stops having any effect:

The explorer needs JavaScript. Use the table below instead.

NumberGap to the next binary64 numberIs x + 1 a different number?
12.22×10−162.22\times10^{-16}yes
10810^{8}1.49×10−81.49\times10^{-8}yes
253≈9.0×10152^{53}\approx9.0\times10^{15}2no
101610^{16}2no
103010^{30}1.41×10141.41\times10^{14}no

Comparing computed numbers

Two numbers that are equal on paper can differ by a few units of rounding, as 0.1 + 0.2 and 0.3 do.
A program that compares computed numbers therefore asks whether they are close, not whether they are equal.
Python’s function math.isclose, called like this:

math.isclose(a, b, rel_tol=1e-09, abs_tol=0.0)

answers True exactly when

∣a−b∣≤max⁡(rel_tol⋅max⁡(∣a∣,∣b∣), abs_tol)|a-b|\le\max\bigl(\text{rel\_tol}\cdot\max(|a|,|b|),\ \text{abs\_tol}\bigr)

Here rel_tol is the largest difference we accept as a fraction of the larger of the two numbers (a relative bound), and abs_tol is a fixed difference in the units of the numbers (an absolute bound).
We call the pair the comparison tolerance. It is not the tolerance of an iteration:
that one is the threshold that stops a method (see the course conventions), while the comparison tolerance decides whether two numbers count as equal.

With only rel_tol, a number can be close to 00 only if it equals 00⁠:
for b=0b=0 the right-hand side is rel_tol⋅∣a∣\text{rel\_tol}\cdot|a|⁠, which is smaller than ∣a−0∣=∣a∣|a-0|=|a| whenever rel_tol is below 1.
This is why the zero case needs abs_tol:

import math
# True: the difference is 5e-10 of the size
print(math.isclose(1e6, 1e6 + 0.0005))
# False: relative to 0 any difference is large
print(math.isclose(1e-15, 0.0))
# True
print(math.isclose(1e-15, 0.0, abs_tol=1e-12))
True
False
True

A tolerance cannot be smaller than the gap

Neighbouring binary64 numbers differ, relative to their size, by at least 2−53≈1.1×10−162^{-53}\approx1.1\times10^{-16}⁠.
A comparison tolerance (or a stopping threshold) below the gap beside the numbers can therefore be met only by numbers that are equal.
Near 1 the gap is 2.2×10−162.2\times10^{-16}⁠, so demanding an error below 10−2010^{-20} for a root near 1 cannot succeed unless the stored number is the exact answer.
The rule: choose tolerances above εmach\varepsilon_{\text{mach}} times the size of the numbers involved.

Rounding a typed decimal

Python’s round(x, n) returns x rounded to n digits after the decimal point:
round(1.26, 1) is 1.3. It acts on the stored binary value of x, not on the digits that were typed.
A decimal such as 2.6752.675⁠, halfway between 2.672.67 and 2.682.68⁠, looks like a tie, but it is a true tie only when the typed value is exactly representable, that is, when it is a fraction whose denominator is a power of 2.
For example 0.125=180.125=\tfrac18 and 2.5=522.5=\tfrac52 are stored exactly, so round(0.125, 2) is a real tie and goes to the even digit, giving 0.12.

The number 2.675=26751000=107402.675=\tfrac{2675}{1000}=\tfrac{107}{40} has the denominator 40=8⋅540=8\cdot5⁠, which is not a power of 2, so it is not stored exactly.
The stored value is 2.67499999999999982…2.67499999999999982\ldots⁠, below the midpoint, and round(2.675, 2) returns 2.67.
The same reasoning decides every decimal tie: the result depends on which side of the midpoint the stored number falls, which looks random from the digits alone.

For money, a cash register must follow a rounding rule fixed in advance.
In Israel, according to the Bank of Israel announcement on cancelling the 5 agorot coin (1 January 2008), only the total of a cash payment is rounded, to ten agorot:
1 to 4 agorot round down and 5 or more round up (17.04 becomes 17.00, and 17.07 becomes 17.10).
A total that ends in 5 agorot, such as 17.05, is exactly halfway between two totals of ten agorot.
That is a tie, and the rule says it rounds up. Let us check what round does with such totals:

for x in [17.05, 17.15, 17.25, 17.45]:
    print(x, round(x, 1))
17.05 17.1
17.15 17.1
17.25 17.2
17.45 17.4
Totalround(x, 1)The ruleWhy
17.0517.117.1agrees by chance: the stored value 17.05000000000000071…17.05000000000000071\ldots is above the midpoint
17.1517.117.2the stored value 17.14999999999999858…17.14999999999999858\ldots is below the midpoint
17.2517.217.3the value is stored exactly, and round settles the tie to the even digit, like 0.125 becoming 0.12 earlier
17.4517.417.5the stored value 17.44999999999999929…17.44999999999999929\ldots is below the midpoint

round acts on the stored binary value and not on the digits that were typed, so it does not follow the rule, and a cash register should not rely on it.

Python’s decimal module keeps decimal digits exactly and lets us name the rule.
Decimal('17.05'), written as a string, holds the typed digits exactly, the method quantize rounds a decimal to the shape of its argument (Decimal('0.1') means one digit after the point), and rounding=ROUND_HALF_UP names the rule:
a tie goes up, away from zero.

from decimal import Decimal, ROUND_HALF_UP
for s in ['17.05', '17.15', '17.25', '17.45']:
    print(s, Decimal(s).quantize(Decimal('0.1'), rounding=ROUND_HALF_UP))
17.05 17.1
17.15 17.2
17.25 17.3
17.45 17.5

Here the typed digits are kept exactly, so 17.05, 17.15, 17.25 and 17.45 are real ties, and the rule we named decides them, unlike round.

A formula for the gap between a stored number and its neighbor

So far we read the gaps off the number line of the tiny format.
Here we compute them for any format, without a figure, and obtain an explanation of two patterns we saw:
the absolute gap grows with the size of the numbers, and the relative gap stays within a fixed range.

In a format with pp fraction bits, a number of the form 2e×1.f2^{e}\times1.f equals 2e−p×(2p+F)2^{e-p}\times(2^{p}+F)⁠, where FF is the whole-number value of the fraction bits.
It is a whole multiple of 2e−p2^{e-p}⁠, the value of the last fraction bit.
The next stored number is obtained when FF grows by 1, that is, when the number grows by 2e−p2^{e-p}⁠.
The gap between two adjacent stored numbers with the same exponent is therefore

gap=2 e−p\text{gap}=2^{\,e-p}

We check the formula in the tiny format (p=3p=3⁠) and in binary64 (p=52p=52⁠):

  • In the tiny format, between 4 and 8 we have e=2e=2⁠, so the gap is 22−3=122^{2-3}=\tfrac12
  • In the tiny format, between 1 and 2 we have e=0e=0⁠, so the gap is 2−3=18=εmach2^{-3}=\tfrac18=\varepsilon_{\text{mach}}
  • In binary64, near 1 the gap is 2−522^{-52}
  • In binary64, near 101610^{16} we have e=53e=53⁠, so the gap is 253−52=22^{53-52}=2

The formula explains both patterns. The absolute gap depends only on ee⁠, so it is constant between two consecutive powers of 2 and doubles when ee grows by 1.
For normal numbers the relative gap is 2e−p2e×1.f=2−p1.f\dfrac{2^{e-p}}{2^{e}\times1.f}=\dfrac{2^{-p}}{1.f}⁠, and as 1.f1.f ranges from 1 to 2 it ranges from 2−p−12^{-p-1} to 2−p2^{-p} (in the tiny format, from 116\tfrac1{16} to 18\tfrac18⁠).
Near 1, that is when e=0e=0⁠, the gap is 2−p=εmach2^{-p}=\varepsilon_{\text{mach}}⁠.

Machine epsilon in the IEEE formats

Machine epsilon is the gap near 1, and by the gap formula with e=0e=0 it equals 2−p2^{-p}⁠.
We substitute the number pp of fraction bits of each format from the table of the standard:

FormatFraction bits ppMachine epsilon
binary16 (half precision)10102−10≈9.77×10−42^{-10}\approx9.77\times10^{-4}
binary32 (single precision)23232−23≈1.19×10−72^{-23}\approx1.19\times10^{-7}
binary64 (double precision)52522−52≈2.22×10−162^{-52}\approx2.22\times10^{-16}

In binary64, machine epsilon is 2−52≈2.22×10−162^{-52}\approx2.22\times10^{-16}⁠, and since this is also the relative gap near 1, a stored binary64 number represents the number we asked for to about 16 significant decimal digits.

Why this gap matters

When we build a method that solves a problem, it computes step by step and stops when the result is close enough to the answer.
Here the computer limits us: stored numbers are not a continuum, and between two neighboring stored numbers there is no stored number at all.
So there is no point in demanding that a computation come closer than this gap:
the demand will never be met, and the method will keep running without stopping, or stop only by accident.

The gap itself grows with the size of the numbers, but the gap relative to the size of the number, that is, the relative error caused by a jump of one gap, hardly changes.
Near 1 the gap and the relative error are the same, so machine epsilon is the unit of accuracy that fits every size:
when the numbers are of size MM⁠, we will not ask for a closeness smaller than εmach⋅M\varepsilon_{\text{mach}}\cdot M⁠.
For example, in binary64 near 1 we will not ask for a closeness smaller than 2.2×10−162.2\times10^{-16}⁠, and near 10810^{8} not smaller than about 2.2×10−82.2\times10^{-8}⁠.

The same principle applies to comparison: two computed numbers that differ by one gap or less are, as far as the computer is concerned, almost the same number.

Continue learning

The rounding puzzle began as two LinkedIn posts, the puzzle and its discussion, whose comments raised the questions these lessons answer.

The next step is the page Computing the Stored Representation: Two Methods, which finds the stored value of 0.1 in two ways, step by step, and uses it for two sums that look alike but behave differently.
After it comes the practice session for both lectures: the inspector with conclusions you commit to, the limits of binary64, safe comparison of numbers, and the rounding puzzle.
After it, the lecture Small Errors, Big Consequences: The Patriot Clock studies how these small rounding errors behave when we compute:
how they accumulate, and why subtracting nearly equal numbers can destroy accuracy.

For a deeper look: