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 of this page:
a sign and five significant bits, written , with exponents large enough that overflow never matters.
It has four fraction bits (), so by the gap rule the gap beside a number is .
Rounding is to the nearest stored number, and a tie goes to the neighbour whose last bit is .
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 , and 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 .
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 has an endless binary expansion, so no finite number of bits stores it exactly.
More bits make the stored number closer to and the error smaller, but never zero.
Numbers whose denominator is a power of 2, such as , 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 and the next stored number above it.
In binary64 it is . It is a gap, so it controls the relative error of a stored number.
The smallest positive normal number is far smaller, about .
The smallest positive number of all (subnormal) is about .
Neither has anything to do with the gap near .
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 (Euler’s number) by adding the terms of the series up to in exact arithmetic; (2) in binary64 we compute for and get ; (3) a clock that adds a tick of the stored 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, .
(2) Round-off: no infinite process is stopped; is stored as and the subtraction erases all the digits.
(3) Round-off: the stored value of is not exact, and this error repeats at every tick and accumulates.
Group B: short calculations by hand
B1. Write in binary. Is it stored exactly in ?
Give the significand and exponent.
Hint
Write as a fraction with a power of 2 in the denominator.
Solution to B1
and , so . The significand has four significant bits, which fits in five, so it is stored exactly, with significand and exponent .
B2. Round to . Give the stored number, the absolute error and the relative error.
Hint
. Keep four bits after the point and look at the bits that follow.
Solution to B2
Four bits after the point give , and the bits that follow, , are more than one half of the last place, so we round up to .
The stored number is . The absolute error is and the relative error is , about .
B3. In the number is stored exactly. What is the distance between and the next stored number?
What does give in , and why?
Solution to B3
Near the last of the five significant bits is worth , so the next stored number is .
The exact sum is closer to than to (distance against ), so the result is :
the 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 is the source of the problem because it has no finite binary expansion.
C2. A clock adds a tick of second at every step. Because is stored with an error, each step is off by seconds.
After how long has the total error reached seconds, the size of the error in the Patriot incident?
Give the answer in hours.
Solution to C2
The error after ticks is seconds, so ticks.
There are 10 ticks per second, so this is seconds, which is hours.
The Patriot lesson gives seconds after 100 hours; the difference is only that is 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 , and each stored value must be within 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 is .
Does binary32 meet the requirement? Does binary64 (53 significant bits)?
Solution to C3
Since , the exponent is . In binary32 the gap is , so the largest rounding error is half of it, .
This is larger than , so binary32 does not meet the requirement.
In binary64 the gap is and half of it is about , far below , 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.