Exercise

Unit 1, questions to solve on paper: numbers and errors

Ten questions in three groups (concepts, short hand calculations, transfer to a new situation) on number representation, rounding and error accumulation, written to be solved on paper without the simulators.

Last updated:


These questions are meant to be solved on paper. Solve each one without the simulators and without running code, then open the solution and compare.
The questions cover number representation, rounding and machine epsilon, and the accumulation of errors.

Three groups, a few questions in each:

  • Concepts: explain in your own words, in two or three sentences.
  • Short calculations by hand: a few lines of binary arithmetic.
  • Transfer: the same ideas in a situation you have not seen.

For the calculations, use the small format FF of this page:
a sign and five significant bits, written 2e×1.b1b2b3b42^{e}\times1.b_1b_2b_3b_4⁠‏, with exponents large enough that overflow never matters.
It has four fraction bits (p=4p=4⁠), so by the gap rule the gap beside a number 2e×1.f2^{e}\times1.f is 2e−42^{e-4}⁠‏.
Rounding is to the nearest stored number, and a tie goes to the neighbour whose last bit is 00⁠‏.

Group A: concepts

A1. In Python, 0.1 + 0.2 == 0.3 is False. Explain why, in two or three sentences, using the words binary expansion, stored and rounding.

Solution to A1

The numbers 0.10.1⁠‏, 0.20.2 and 0.30.3 have binary expansions that never end, so none of them can be stored exactly:
each is replaced by the nearest stored number. The sum of the two stored numbers is then rounded again, and the result happens to be a different stored number from the one stored for 0.30.3⁠‏.
The three roundings do not cancel here, which is why the comparison is False.

A2. True or false, with a reason: “In a format with more significant bits, every decimal number with a few digits is stored exactly.”

Solution to A2

False. A decimal such as 0.10.1 has an endless binary expansion, so no finite number of bits stores it exactly.
More bits make the stored number closer to 0.10.1 and the error smaller, but never zero.
Numbers whose denominator is a power of 2, such as 0.625=580.625=\tfrac58⁠‏, are stored exactly as soon as there are enough bits.

A3. What does machine epsilon measure, and what is its value in binary64?
Why is it not “the smallest positive number a float can store”?

Solution to A3

Machine epsilon is the gap between 11 and the next stored number above it.
In binary64 it is 2−52≈2.2×10−162^{-52}\approx2.2\times10^{-16}⁠‏. It is a gap, so it controls the relative error of a stored number.
The smallest positive normal number is far smaller, about 2.2×10−3082.2\times10^{-308}⁠‏.
The smallest positive number of all (subnormal) is about 4.9×10−3244.9\times10^{-324}⁠‏.
Neither has anything to do with the gap near 11⁠‏.

A4. In each of the following situations decide whether the source of the deviation is a truncation error or a round-off error, and justify in one sentence:
(1) we approximate the number ee (Euler’s number) by adding the terms of the series ∑k=0∞1k!=1+1+12+16+124+⋯\sum_{k=0}^{\infty}\frac1{k!}=1+1+\frac12+\frac16+\frac1{24}+\cdots up to 14!\tfrac1{4!} in exact arithmetic; (2) in binary64 we compute 1−cos⁡xx2\dfrac{1-\cos x}{x^2} for x=10−8x=10^{-8} and get 00⁠; (3) a clock that adds a tick of the stored 0.10.1 millions of times drifts.

Solution to A4

(1) Truncation: the arithmetic is exact and the deviation comes from the omitted terms, about the size of the first omitted term, 15!≈8×10−3\tfrac1{5!}\approx8\times10^{-3}⁠.
(2) Round-off: no infinite process is stopped; cos⁡x\cos x is stored as 11 and the subtraction erases all the digits.
(3) Round-off: the stored value of 0.10.1 is not exact, and this error repeats at every tick and accumulates.

Group B: short calculations by hand

B1. Write 0.81250.8125 in binary. Is it stored exactly in FF⁠‏?
Give the significand and exponent.

Hint

Write 0.81250.8125 as a fraction with a power of 2 in the denominator.

Solution to B1

0.8125=13160.8125=\tfrac{13}{16} and 13=1101213=1101_2⁠‏, so 0.8125=0.11012=2−1×1.10120.8125=0.1101_2=2^{-1}\times1.101_2⁠‏. The significand 1.1011.101 has four significant bits, which fits in five, so it is stored exactly, with significand 1.10101.1010 and exponent −1-1⁠‏.

B2. Round 0.10.1 to FF⁠‏. Give the stored number, the absolute error and the relative error.

Hint

0.1=2−4×1.1001 1001 1001…20.1=2^{-4}\times1.1001\,1001\,1001\ldots_2⁠‏. Keep four bits after the point and look at the bits that follow.

Solution to B2

Four bits after the point give 1.10011.1001⁠‏, and the bits that follow, 1001…21001\ldots_2⁠‏, are more than one half of the last place, so we round up to 1.10101.1010⁠‏.
The stored number is 2−4×1.10102=1.625×116=13128=0.10156252^{-4}\times1.1010_2=1.625\times\tfrac1{16}=\tfrac{13}{128}=0.1015625⁠‏. The absolute error is 0.1015625−0.1=0.00156250.1015625-0.1=0.0015625‏ and the relative error is 0.00156250.1=0.015625=2−6\frac{0.0015625}{0.1}=0.015625=2^{-6}⁠‏, about 1.6%1.6\%⁠‏.

B3. In FF the number 88 is stored exactly. What is the distance between 88 and the next stored number?
What does 8+0.28+0.2 give in FF⁠‏, and why?

Solution to B3

Near 8=23×1.000028=2^{3}\times1.0000_2 the last of the five significant bits is worth 23−4=0.52^{3-4}=0.5⁠‏, so the next stored number is 8.58.5⁠‏.
The exact sum 8.28.2 is closer to 88 than to 8.58.5 (distance 0.20.2 against 0.30.3⁠)‏, so the result is 88⁠‏:
the 0.20.2 is absorbed, as a number smaller than half the gap is when it is added to a larger one.

Group C: transfer

C1. A shop’s program adds ten prices of 0.10 and compares the total with 1.00.
It sometimes reports a difference. Propose a way to store the prices so that the comparison is exact, and say why it works.

Solution to C1

Store the prices as whole numbers of cents: a price of 10 cents is added ten times and compared with 100.
Or use a decimal type. Integers are stored exactly and their sums are exact, so there is no rounding.
The decimal 0.100.10 is the source of the problem because it has no finite binary expansion.

C2. A clock adds a tick of 0.10.1 second at every step. Because 0.10.1 is stored with an error, each step is off by 9.54×10−89.54\times10^{-8} seconds.
After how long has the total error reached 0.340.34 seconds, the size of the error in the Patriot incident?
Give the answer in hours.

Solution to C2

The error after NN ticks is N×9.54×10−8N\times9.54\times10^{-8} seconds, so N=0.349.54×10−8≈3.56×106N=\frac{0.34}{9.54\times10^{-8}}\approx3.56\times10^{6} ticks.
There are 10 ticks per second, so this is 3.56×1053.56\times10^{5} seconds, which is 3.56×1053600≈99\frac{3.56\times10^{5}}{3600}\approx99 hours.
The Patriot lesson gives 0.34330.3433 seconds after 100 hours; the difference is only that 0.340.34 is 0.34330.3433 rounded.
A rounding error that looks negligible at each step becomes large when it accumulates over a long run.

C3. A sensor reports values near 10610^{6}⁠‏, and each stored value must be within 0.010.01 of the true value.
A stored number is at most half the gap beside it away from the number it replaces.
In binary32 (24 significant bits) the gap between stored numbers near 10610^{6} is 219−232^{19-23}⁠‏.
Does binary32 meet the requirement? Does binary64 (53 significant bits)?

Solution to C3

Since 219=524288≤106<2202^{19}=524288\le10^{6}<2^{20}⁠‏, the exponent is 1919⁠‏. In binary32 the gap is 219−23=2−4=0.06252^{19-23}=2^{-4}=0.0625⁠‏, so the largest rounding error is half of it, 0.031250.03125⁠‏.
This is larger than 0.010.01⁠‏, so binary32 does not meet the requirement.
In binary64 the gap is 219−52≈1.2×10−102^{19-52}\approx1.2\times10^{-10}‏ and half of it is about 6×10−116\times10^{-11}⁠‏, far below 0.010.01⁠‏, so binary64 meets it.

Continue learning

Return to How Computers Store Numbers or Small Errors, Big Consequences for the explanations, and to the glossary for the terms.