Exercise

Computing the Stored Representation: Two Methods

Two ways to find the binary64 number the computer stores for a decimal, step by step, and two sums that look alike but behave differently.

Last updated:


After this page you will be able to:

  • find the number that binary64 stores for a decimal, in two different ways, and check that the two ways agree;
  • tell apart the rounding that happens when a number is stored from the rounding that happens when an addition finishes;
  • explain why 0.04 + 0.07 == 0.11 is False and 0.1 + 0.3 == 0.4 is True, although none of these six numbers is stored exactly.

It follows the two lectures How Computers Store Numbers and Back to the Question.
The second lecture showed one of the methods in a few lines.
Here both are done slowly, with the same example.

What we compute

When you type a decimal such as 0.10.1⁠, the computer stores a nearby number that binary64 can hold.
We write fl⁡(x)\operatorname{fl}(x) for the stored value of xx (“fl” stands for floating point).
The goal of this page is to find fl⁡(x)\operatorname{fl}(x) exactly.

Recall the format: a normal binary64 number has a sign, an exponent and a fraction, in that order, and stands for

(−1)s×2e×1.f,(-1)^{s}\times2^{e}\times1.f,

where the fraction ff has 52 bits, the leading 11 before the point is not stored, and the exponent field holds e+1023e+1023 (the bias).
Our running example is 0.10.1⁠. Both methods must lead to the same stored value.

Method A: expand in binary

Steps 1 to 3: the binary expansion

  1. Write the number as an exact fraction. 0.1=1100.1=\tfrac{1}{10}⁠.

  2. Expand it in binary with the doubling procedure from the lecture:
    double the number, the integer part is the next bit, and continue with the fractional part.
    For 110\tfrac{1}{10} the first steps are:

    StepNumber after doublingNext bitFractional part kept
    10.200.2
    20.400.4
    30.800.8
    41.610.6
    51.210.2
  3. Spot the repetition. After step 5 we are back at 0.20.2⁠, the number we had after step 1.
    The procedure depends only on the current number, so from here the bits repeat forever.
    The repeating block is 00110011⁠:

    0.1=0.00011‾2=0.0001 1001 1001 1001…2.0.1=0.0\overline{0011}_2=0.0001\,1001\,1001\,1001\ldots_2 .

    The expansion never ends, so 0.1 cannot be stored exactly. Everything below depends on this.

Steps 4 to 6: the form with a leading 1, and the fraction bits

  1. Move the point to the form 2e×1.f2^{e}\times1.f⁠. The first 11 is in the 4th place after the point.
    Shifting the point four places to the right multiplies by 242^{4}⁠, so we compensate with 2−42^{-4}⁠:

    0.1=2−4×1.1001 1001 1001…2,e=−4.0.1=2^{-4}\times1.1001\,1001\,1001\ldots_2 ,\qquad e=-4 .
  2. Drop the leading 1. It is not stored. The fraction ff is what follows it:
    1001 1001 1001…1001\,1001\,1001\ldots⁠, repeating forever.

  3. Fill the 52 fraction bits. Fifty-two bits are thirteen groups of four, so the kept part is thirteen groups 10011001⁠:

    1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001

    The expansion does not stop here. It goes on with 1001 1001…1001\,1001\ldots⁠, and these bits are cut off.

Steps 7 and 8: rounding and the stored value

  1. Look at what was cut and decide the rounding. The cut-off part is 0.1001 1001…20.1001\,1001\ldots_2⁠. A repeating group of four bits equals its value as a whole number (here 10012=91001_2=9⁠) divided by 24−1=152^4-1=15⁠, so this part equals 915=0.6\tfrac{9}{15}=0.6 of the last kept place, more than half: the first cut bit is 1 and another 1 follows. So we round up: add 1 to the last group, 1001+1=10101001+1=1010⁠. The stored fraction is twelve groups 10011001 followed by 10101010⁠.
  1. Compute the stored value without writing out all the bits. The expansion of 0.1 is a geometric series, so we can write it in two forms:

    0.1=(2−4+2−5)∑k=0∞16−k=2−4+2−5+2−8+2−9+2−12+2−13+⋯\begin{aligned} 0.1&=(2^{-4}+2^{-5})\sum_{k=0}^{\infty}16^{-k}\\ &=2^{-4}+2^{-5}+2^{-8}+2^{-9}+2^{-12}+2^{-13}+\cdots \end{aligned}

    The stored number contains the 13 terms with k=0,…,12k=0,\ldots,12 in full.
    From the 14th term (k=13k=13⁠) only its first bit is kept, 2−562^{-56} (the last kept bit), plus one more 2−562^{-56} from rounding up; together they are 2−552^{-55}⁠, the last term in the formula below.
    The sum of the 13 terms is a finite geometric series. A sum of nn terms, each one the previous term times qq⁠, has a formula:

    ∑k=0n−1qk=1+q+q2+⋯+qn−1=1−qn1−q\sum_{k=0}^{n-1}q^{k}=1+q+q^{2}+\cdots+q^{n-1}=\frac{1-q^{n}}{1-q}

    Here q=16−1q=16^{-1} and n=13n=13⁠, the factor in front of the sum is 2−4+2−5=3322^{-4}+2^{-5}=\tfrac{3}{32}⁠, and 16−13=2−5216^{-13}=2^{-52}⁠:

    fl⁡(0.1)=(2−4+2−5)∑k=01216−k+2−55=332⋅1−16−131−16−1+2−55=332⋅1615 (1−2−52)+2−55=0.1 (1−2−52)+2−55.\begin{aligned} \operatorname{fl}(0.1)&=(2^{-4}+2^{-5})\sum_{k=0}^{12}16^{-k}+2^{-55}\\ &=\frac{3}{32}\cdot\frac{1-16^{-13}}{1-16^{-1}}+2^{-55}\\ &=\frac{3}{32}\cdot\frac{16}{15}\,(1-2^{-52})+2^{-55}\\ &=0.1\,(1-2^{-52})+2^{-55}. \end{aligned}

    Since 0.1×2−52=1.6×2−560.1\times2^{-52}=1.6\times2^{-56} and 2−55=2×2−562^{-55}=2\times2^{-56}⁠:

    fl⁡(0.1)−0.1=(2−1.6)×2−56=0.4×2−56≈5.55×10−18\begin{aligned} \operatorname{fl}(0.1)-0.1&=(2-1.6)\times2^{-56}\\ &=0.4\times2^{-56}\approx5.55\times10^{-18} \end{aligned}

    The cut-off tail was 0.60.6 of the last place, and rounding up added one whole place, so the stored value is 1−0.6=0.41-0.6=0.4 of a last place above 0.10.1⁠.

Method B: a whole number with 53 bits

  1. Write x=2e×mx=2^{e}\times m with 1≤m<21\le m<2⁠. For 0.10.1⁠: 2−4=116=0.06252^{-4}=\tfrac{1}{16}=0.0625 and 0.1÷0.0625=1.60.1\div0.0625=1.6⁠, so 0.1=2−4×1.60.1=2^{-4}\times1.6⁠.

  2. Multiply mm by 2522^{52}⁠. Binary64 keeps 52 bits after the point of mm⁠.
    Multiplying by 2522^{52} moves the point 52 places to the right:
    the kept bits become the whole part and the cut-off bits become the fractional part.
    Since 252=45035996273704962^{52}=4503599627370496⁠, we multiply by 1616 and divide by 1010⁠:

    1.6×252=16×450359962737049610=7205759403792793.6.\begin{aligned} 1.6\times2^{52}&=\frac{16\times4503599627370496}{10}\\ &=7205759403792793.6 . \end{aligned}
  3. Round to the nearest whole number, by the IEEE 754 rule (tie to even: a number exactly halfway between two whole numbers goes to the even one).
    The fractional part 0.60.6 is more than half, so we round up.
    Call the result MM⁠:

    M=7205759403792794.M=7205759403792794 .

    This is the same decision as step 7 of Method A: the fractional part 0.60.6 is exactly the cut-off tail 0.1001 1001…20.1001\,1001\ldots_2⁠.

  4. The stored value is M×2e−52M\times2^{e-52}⁠:

    fl⁡(0.1)=7205759403792794×2−56.\operatorname{fl}(0.1)=7205759403792794\times2^{-56}.
  5. If needed, recover the fields. The sign is 00⁠. The exponent field is e+1023=1019e+1023=1019⁠.
    The fraction field is M−252M-2^{52} written in 52 bits: the leading 1 of MM is not stored.

    The error is M−m⋅252=7205759403792794−7205759403792793.6=0.4M-m\cdot2^{52}=7205759403792794-7205759403792793.6=0.4 of a last place, as in Method A.

Check it in Python

The function below finds MM and the power of 2 for any number.
math.frexp splits a number as m×2km\times2^{k} with 0.5≤m<10.5\le m<1⁠, so m×253m\times2^{53} is a whole number with 53 bits.
The function int(y) drops the fraction of y and keeps its whole part, and the method hex shows the stored bits in groups of four (one hexadecimal digit per group: 9 is 10011001 and a is 10101010⁠).

import math

def stored(x):
    # x = m * 2**k with 0.5 <= m < 1
    m, k = math.frexp(x)
    # x = M * 2**(k - 53), M a whole number
    return int(m * 2**53), k - 53

# (M, power of 2)
print(stored(0.1))
# the stored bits, in groups of four
print((0.1).hex())

You should see (7205759403792794, -56) and 0x1.999999999999ap-4:
twelve groups 9, then a, and the exponent −4-4⁠, exactly as in Method A.

Which method when

Method A: expand in binaryMethod B: a 53-bit whole number
Showswhy the expansion never ends, the repeating sequence, which bits are cutthe exact stored value and the exact error, with whole-number arithmetic
Suited toseeing the structure of the bitscalculations, and comparing stored values
Drawbacklong for numbers with a long repeating sequencehides the bits

Use A to understand a number, and B to compute with it. They always agree.

Two different roundings

A sum of two decimals involves two roundings, and the exercises below depend on keeping them apart:

  1. When a number is stored. The decimal you type is replaced by the nearest stored number, fl⁡(x)\operatorname{fl}(x)⁠.
  2. When an operation finishes. The computer adds the two stored numbers (think of it as an exact addition), and the exact sum may not be a stored number, so it is rounded again to the nearest stored number. A tie, a sum exactly halfway between two stored numbers, goes to the neighbour whose last bit is 00 (“ties to even”).

In class: is 0.04+0.07=0.110.04+0.07=0.11⁠?

Python says 0.04 + 0.07 == 0.11 is False. Explain why, with exact numbers, without Python:

  1. Find the stored values fl⁡(0.04)\operatorname{fl}(0.04)⁠, fl⁡(0.07)\operatorname{fl}(0.07) and fl⁡(0.11)\operatorname{fl}(0.11) with Method B, each as M×2kM\times2^{k}⁠.
  2. For each, say whether the stored value is above or below the decimal, and by how much of a last place.
  3. Add the two stored addends exactly. Put them on the same power of 2 first.
  4. Between which two stored numbers does the exact sum lie? Is it a tie?
  5. What does the addition return, and is it fl⁡(0.11)\operatorname{fl}(0.11)⁠?
Hint: the three decompositions

0.04=2−5×1.280.04=2^{-5}\times1.28⁠, 0.07=2−4×1.120.07=2^{-4}\times1.12 and 0.11=2−4×1.760.11=2^{-4}\times1.76⁠. Multiply each mm by 252=45035996273704962^{52}=4503599627370496 and round.

Solution

1. The stored values.

Numberm×252m\times2^{52}MMStored valueDirection
0.04=2−5×1.280.04=2^{-5}\times1.285764607523034234.885764607523034234.8857646075230342355764607523034235M×2−57M\times2^{-57}up by 0.120.12
0.07=2−4×1.120.07=2^{-4}\times1.125044031582654955.525044031582654955.5250440315826549565044031582654956M×2−56M\times2^{-56}up by 0.480.48
0.11=2−4×1.760.11=2^{-4}\times1.767926335344172072.967926335344172072.9679263353441720737926335344172073M×2−56M\times2^{-56}up by 0.040.04

All three are stored above the decimal. (“Up by 0.12” means 0.12 of a last place, which is 2−572^{-57} for 0.04 and 2−562^{-56} for the others.)

2. The exact sum of the stored addends. Write both on the power 2−572^{-57}⁠:
fl⁡(0.07)=10088063165309912×2−57\operatorname{fl}(0.07)=10088063165309912\times2^{-57}⁠. Then

fl⁡(0.04)+fl⁡(0.07)=(5764607523034235+10088063165309912)×2−57=15852670688344147×2−57=7926335344172073.5×2−56\begin{aligned} &\operatorname{fl}(0.04)+\operatorname{fl}(0.07)\\ &\quad=(5764607523034235+10088063165309912)\times2^{-57}\\ &\quad=15852670688344147\times2^{-57}\\ &\quad=7926335344172073.5\times2^{-56} \end{aligned}

3. A tie. The sum is in the same range as 0.110.11⁠, where stored numbers are multiples of 2−562^{-56}⁠.
Its neighbours are 7926335344172073×2−567926335344172073\times2^{-56} (this is fl⁡(0.11)\operatorname{fl}(0.11)⁠) and 7926335344172074×2−567926335344172074\times2^{-56}⁠, and it is exactly halfway between them.

4. Ties to even. The even neighbour is 79263353441720747926335344172074⁠, so the addition returns 7926335344172074×2−567926335344172074\times2^{-56}⁠, one place above fl⁡(0.11)\operatorname{fl}(0.11)⁠.
The comparison is False, and Python prints the sum as 0.11000000000000001.

Why. The three representation errors all point up. In units of 2−562^{-56} the stored addends have errors 0.060.06 (for 0.04) and 0.480.48⁠, together 0.540.54⁠, while fl⁡(0.11)\operatorname{fl}(0.11) has error 0.040.04⁠.
So the exact sum sits 0.54−0.04=0.50.54-0.04=0.5 above fl⁡(0.11)\operatorname{fl}(0.11)⁠: exactly halfway to the next number, and the tie goes the wrong way.

Check it in Python:

print(0.04 + 0.07 == 0.11)
print(0.04 + 0.07)
# the last digit differs by one
print((0.11).hex(), (0.04 + 0.07).hex())

Homework: equality that holds although nothing is exact

Python says 0.1 + 0.3 == 0.4 is True, although none of the three numbers is stored exactly.

  1. Explain it with the stored values and the rounding of the sum, as in the class exercise. Write your reasoning before you look at an explorer: the Addition explorer on the practice page will show the answer at once.
  2. Find three other numbers a,b,ca,b,c⁠, none stored exactly, for which a + b == c is True in Python. (Scaling the example above by 2 changes only the exponents, so 0.2+0.6=0.80.2+0.6=0.8 does not count as a new idea. Look for another mechanism.)
Hint for question 1

Decompose 0.1=2−4×1.60.1=2^{-4}\times1.6⁠, 0.3=2−2×1.20.3=2^{-2}\times1.2 and 0.4=2−2×1.60.4=2^{-2}\times1.6⁠. Put the stored 0.10.1 on the power 2−542^{-54}⁠, as for 0.30.3 and 0.40.4⁠, before you add.

Hint for question 2

Search with a loop. For each pair of digits i and j, take a = i / 10, b = j / 10 and c = (i + j) / 10, and test a + b == c.
Also test that none of the three is stored exactly: a decimal is stored exactly only if, as a fraction in lowest terms, its denominator is a power of 2.

Solution to question 1
Numberm×252m\times2^{52}MMStored valueDirection
0.1=2−4×1.60.1=2^{-4}\times1.67205759403792793.67205759403792793.672057594037927947205759403792794M×2−56M\times2^{-56}up by 0.40.4
0.3=2−2×1.20.3=2^{-2}\times1.25404319552844595.25404319552844595.254043195528445955404319552844595M×2−54M\times2^{-54}down by 0.20.2
0.4=2−2×1.60.4=2^{-2}\times1.67205759403792793.67205759403792793.672057594037927947205759403792794M×2−54M\times2^{-54}up by 0.40.4

On the power 2−542^{-54} the stored 0.10.1 is 7205759403792794÷4=1801439850948198.57205759403792794\div4=1801439850948198.5⁠. The exact sum of the stored addends is

1801439850948198.5×2−54+5404319552844595×2−54=7205759403792793.5×2−54\begin{aligned} &1801439850948198.5\times2^{-54}\\ &\quad+5404319552844595\times2^{-54}\\ &\quad=7205759403792793.5\times2^{-54} \end{aligned}

This is a tie between 72057594037927937205759403792793 and 72057594037927947205759403792794⁠. The even one is 72057594037927947205759403792794⁠, which is exactly fl⁡(0.4)\operatorname{fl}(0.4)⁠.
So the addition returns fl⁡(0.4)\operatorname{fl}(0.4) and the comparison is True.

Why. Here the errors go in opposite directions (0.1 up, 0.3 down), and they nearly cancel.
The exact sum falls exactly halfway between two stored numbers, and ties to even picks the one that is also the stored 0.40.4⁠.
In the class exercise the same tie picked the wrong neighbour.
The equality does not depend on whether the numbers are exact:
it depends on where the exact sum falls and on the parity of the neighbours.

Solution to question 2

Among decimals with one digit after the point, these also give True with all three numbers inexact:

  • 0.1 + 0.6 == 0.7
  • 0.3 + 0.4 == 0.7
  • 0.1 + 0.8 == 0.9

The mechanisms differ. In 0.3+0.4=0.70.3+0.4=0.7 the exact sum of the stored addends is a tie, as for 0.1+0.30.1+0.3⁠.
In 0.1+0.6=0.70.1+0.6=0.7 and 0.1+0.8=0.90.1+0.8=0.9 it is not a tie: the exact sum is simply nearer to the stored cc than to any other stored number.
For comparison, 0.1 + 0.7 == 0.8 is False.

A loop that finds them. Fraction(x) (from the fractions module) is the exact value of the stored number x, str(x) is the number written as text, with the digits that print shows, so Fraction(str(x)) is the exact value of those digits, and for i in range(1, 10) repeats with i=1,2,…,9i=1,2,\dots,9⁠:

from fractions import Fraction

# True if the decimal is stored exactly
def exact(x):
    return Fraction(x) == Fraction(str(x))

for i in range(1, 10):
    for j in range(i, 10 - i):
        a, b, c = i / 10, j / 10, (i + j) / 10
        if (
            a + b == c
            and not (exact(a) or exact(b) or exact(c))
        ):
            print(a, b, c)

The loop also prints doublings such as 0.1+0.1=0.20.1+0.1=0.2 and scaled copies such as 0.2+0.6=0.80.2+0.6=0.8⁠, where only the exponent changes.
They are true, but they teach nothing new.

Continue learning

The next step is the practice session for these lectures: the inspector, the Addition explorer, safe comparison of floating-point numbers and the rounding puzzle.