AQA 7517, section 4.5.4.5
A computer stores each number in a fixed number of bits. That means many numbers with a fractional part can't be stored exactly, and are stored as a close approximation instead. The difference between the true value and the stored one is a rounding error.
Why do rounding errors happen?
Binary fractions use place values , , , , and so on. For a real number to be represented exactly, it must be possible to write it as a binary fraction in the number of bits available.
There are two reasons it can't be done:
- The number needs more bits than are available. For example, needs 4 places after the point. With only 3 fractional bits, it would be rounded.
- The number can never be written exactly in binary. Fractions whose denominators are not powers of 2 (such as and ) give a binary expansion that recurs forever.
| Decimal | Binary | Exact in a finite number of bits? |
|---|---|---|
| 0.5 | 0.1 | Yes |
| 0.25 | 0.01 | Yes |
| 0.75 | 0.11 | Yes |
| 0.1 | 0.000110011001100... | No: it recurs |
| 0.3 | 0.0100110011001... | No: it recurs |
So some values, such as , can never be represented exactly, however many bits are used. This is just as can't be written exactly as a decimal.
Example: storing 0.1
With 8 bits after the binary point, the place values are down to . The nearest value to 0.1 is
This is not 0.1. The stored value has a small error of .
Using more bits gets closer, but it never becomes exactly 0.1.
Both fixed and floating point
Rounding errors affect both representations.
- Fixed point: the number of bits after the point limits the precision. Anything smaller than the last place is lost.
- Floating point: the mantissa has a limited number of bits, so only that many significant bits are kept.
Effects in programs
Errors can build up when calculations are repeated, and comparisons can fail.
print(0.1 + 0.2) # 0.30000000000000004
print(0.1 + 0.2 == 0.3) # False
total = 0
for _ in range(10):
total = total + 0.1
print(total) # 0.9999999999999999
How to deal with it
- Don't test floats for exact equality. Check that the difference is very small instead, such as
abs(a - b) < 0.000001. - Use integers for money. Store pounds as whole pence (499 for £4.99), so the values are exact.
- Round the final result for display, such as
round(total, 2). - Use a data type with more bits (more precision) if accuracy matters.
Worked example
Task: explain why a program that adds 0.1 to a total ten times doesn't reach exactly 1.0.
The number 0.1 can't be stored exactly in binary, because its binary form recurs forever (0.000110011...). Each addition of 0.1 actually adds the stored approximation, which is very slightly different. After ten additions the tiny errors have accumulated, so the total is 0.9999999999999999 rather than 1.0. A comparison total == 1.0 would give False.
Exam tip: give the reason precisely: only numbers that can be written as a binary fraction in the available bits can be stored exactly, and some values, such as , can never be stored exactly. Mention a consequence, such as errors building up or equality tests failing.
Check yourself
Why can the decimal number 0.1 not be stored exactly in binary?
In binary, 0.1 is a recurring fraction (0.000110011...), so it would need an infinite number of bits to be exact, and only a finite number are available.
Give one decimal fraction that can be stored exactly in binary
Any fraction with a power of 2 as its denominator, such as 0.5, 0.25 or 0.375.
How can a program avoid rounding errors when working with money?
Store money as a whole number of pence (an integer), so the calculations are exact.
Why is it unwise to test whether two floating point numbers are exactly equal?
Rounding errors mean two values that should be equal may differ by a tiny amount, so the test may wrongly give false.