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.11isFalseand0.1 + 0.3 == 0.4isTrue, 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 , the computer stores a nearby number that binary64 can hold.
We write for the stored value of (“fl” stands for floating point).
The goal of this page is to find exactly.
Recall the format: a normal binary64 number has a sign, an exponent and a fraction, in that order, and stands for
where the fraction has 52 bits, the leading before the point is not stored, and the exponent field holds (the bias).
Our running example is . Both methods must lead to the same stored value.
Method A: expand in binary
Steps 1 to 3: the binary expansion
-
Write the number as an exact fraction. .
-
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 the first steps are:Step Number after doubling Next bit Fractional part kept 1 0.2 0 0.2 2 0.4 0 0.4 3 0.8 0 0.8 4 1.6 1 0.6 5 1.2 1 0.2 -
Spot the repetition. After step 5 we are back at , 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 :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
-
Move the point to the form . The first is in the 4th place after the point.
Shifting the point four places to the right multiplies by , so we compensate with : -
Drop the leading 1. It is not stored. The fraction is what follows it:
, repeating forever. -
Fill the 52 fraction bits. Fifty-two bits are thirteen groups of four, so the kept part is thirteen groups :
1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001 1001The expansion does not stop here. It goes on with , and these bits are cut off.
Steps 7 and 8: rounding and the stored value
- Look at what was cut and decide the rounding. The cut-off part is . A repeating group of four bits equals its value as a whole number (here ) divided by , so this part equals 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, . The stored fraction is twelve groups followed by .
-
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:
The stored number contains the 13 terms with in full.
From the 14th term () only its first bit is kept, (the last kept bit), plus one more from rounding up; together they are , the last term in the formula below.
The sum of the 13 terms is a finite geometric series. A sum of terms, each one the previous term times , has a formula:Here and , the factor in front of the sum is , and :
Since and :
The cut-off tail was of the last place, and rounding up added one whole place, so the stored value is of a last place above .
Method B: a whole number with 53 bits
-
Write with . For : and , so .
-
Multiply by . Binary64 keeps 52 bits after the point of .
Multiplying by 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 , we multiply by and divide by : -
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 is more than half, so we round up.
Call the result :This is the same decision as step 7 of Method A: the fractional part is exactly the cut-off tail .
-
The stored value is :
-
If needed, recover the fields. The sign is . The exponent field is .
The fraction field is written in 52 bits: the leading 1 of is not stored.The error is of a last place, as in Method A.
Check it in Python
The function below finds and the power of 2 for any number.math.frexp splits a number as with , so 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 and a is ).
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 , exactly as in Method A.
Which method when
| Method A: expand in binary | Method B: a 53-bit whole number | |
|---|---|---|
| Shows | why the expansion never ends, the repeating sequence, which bits are cut | the exact stored value and the exact error, with whole-number arithmetic |
| Suited to | seeing the structure of the bits | calculations, and comparing stored values |
| Drawback | long for numbers with a long repeating sequence | hides 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:
- When a number is stored. The decimal you type is replaced by the nearest stored number, .
- 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 (“ties to even”).
In class: is ?
Python says 0.04 + 0.07 == 0.11 is False. Explain why, with exact numbers, without Python:
- Find the stored values , and with Method B, each as .
- For each, say whether the stored value is above or below the decimal, and by how much of a last place.
- Add the two stored addends exactly. Put them on the same power of 2 first.
- Between which two stored numbers does the exact sum lie? Is it a tie?
- What does the addition return, and is it ?
Hint: the three decompositions
, and . Multiply each by and round.
Solution
1. The stored values.
| Number | Stored value | Direction | ||
|---|---|---|---|---|
| up by | ||||
| up by | ||||
| up by |
All three are stored above the decimal. (“Up by 0.12” means 0.12 of a last place, which is for 0.04 and for the others.)
2. The exact sum of the stored addends. Write both on the power :
. Then
3. A tie. The sum is in the same range as , where stored numbers are multiples of .
Its neighbours are (this is ) and , and it is exactly halfway between them.
4. Ties to even. The even neighbour is , so the addition returns , one place above .
The comparison is False, and Python prints the sum as 0.11000000000000001.
Why. The three representation errors all point up. In units of the stored addends have errors (for 0.04) and , together , while has error .
So the exact sum sits above : 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.
- 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.
- Find three other numbers , none stored exactly, for which
a + b == cisTruein Python. (Scaling the example above by 2 changes only the exponents, so does not count as a new idea. Look for another mechanism.)
Hint for question 1
Decompose , and . Put the stored on the power , as for and , 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
| Number | Stored value | Direction | ||
|---|---|---|---|---|
| up by | ||||
| down by | ||||
| up by |
On the power the stored is . The exact sum of the stored addends is
This is a tie between and . The even one is , which is exactly .
So the addition returns 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 .
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.70.3 + 0.4 == 0.70.1 + 0.8 == 0.9
The mechanisms differ. In the exact sum of the stored addends is a tie, as for .
In and it is not a tie: the exact sum is simply nearer to the stored 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 :
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 and scaled copies such as , 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.