Article
Overflow and roundoff errors
Think of the trip counter in an old car. It has six digits. When it shows 999999 and the car drives one more kilometre, it cannot show 1000000. It shows 000000. The counter ran out of room.
Computers have the same problem, because every number they store has a fixed size.
A number box with a fixed size
A computer keeps a whole number in a fixed number of bits. You already know that n bits can make 2^n different patterns. The patterns start at 0, so the largest whole number is one less than that.
| Bits | Different values | Largest whole number |
|---|---|---|
| 4 | 16 | 15 |
| 8 | 256 | 255 |
| 16 | 65,536 | 65,535 |
| 32 | 4,294,967,296 | 4,294,967,295 |
Now try it yourself. The widget below has 4 bits. Use it to add 1 again and again, and watch what happens after the biggest number.
0000 = 0 (largest with 4 bits: 15)
What happened is called overflow. An overflow happens when a calculation gives a result that is too big for the bits that were set aside for it. Different systems react in different ways. Some wrap around to 0, like the widget. Some stop with an error. In every case, the correct answer is lost.
The fix is simple: use more bits. Each extra bit doubles how many values fit. But the box is still fixed, so a bigger number can still overflow it one day.
Check your understanding
A number is stored in 5 bits. What is the largest whole number it can hold?
Numbers with a point: roundoff
Some numbers cannot be written exactly, even with many digits. In decimal, one third is 0.3333333 and the threes never stop. You must cut it off somewhere, and then the number is a little wrong.
Computers have the same problem in binary. The number 0.1 looks simple to us. In binary it is a pattern that repeats forever, like one third in decimal. The computer has only a fixed number of bits, so it keeps the closest value it can fit. This small difference is called a roundoff error.
Run the code below. You don't need to understand the code yet. It asks the computer to add two numbers and show the answer.
Look carefully at the answer. Is it exactly what you expect from school maths? Edit the numbers and try some other sums, such as 0.5 + 0.25. Some numbers are stored exactly and some are not.
The error is tiny. But small errors can add up when a program does many calculations, and two numbers that should be equal may not match exactly.
Check your understanding
Which of the following describes a roundoff error?
Worked example
A game keeps the number of coins in 8 bits, so the largest value is 255. A player has 250 coins and picks up 10 more.
- The true total is 250 + 10 = 260.
- 260 is bigger than 255, so it does not fit.
- In a system that wraps around, the counter goes 255, then 0, 1, 2 and so on. The new value is 260 − 256 = 4.
Overflow in an 8-bit coin counter
250 coins
the biggest value is 255
pick up 10True total 260
needs more than 8 bits
does not fitCounter wraps
255, then 0, 1, 2, 3, 4
storedShows 4 coins
the right answer is lost
The player now seems to have 4 coins. This is an overflow. The fix is to store the coins in more bits, for example 16.
Two different limits
Overflow
- the result is too big
- it may wrap around or give an error
- fix: use more bits
Roundoff
- the number is not exact
- 0.1 repeats forever in binary
- tiny errors can add up
Common mistakes
- Thinking overflow needs huge numbers. The limit depends on the number of bits. With 4 bits, 16 is already too big.
- Counting the values, not the largest number. 8 bits make 256 values, but the largest number is 255, because counting starts at 0.
- Thinking more bits removes all roundoff. More bits make the stored value closer to the true one. Some numbers still cannot be stored exactly.
- Mixing the two errors. Overflow means the number is too big. Roundoff means the number is not exact. A number can be small and still have a roundoff error.
On the exam
The exam tells you that a computer uses a fixed number of bits for numbers. Then it asks what can go wrong. Remember: a result that is too big for the bits is an overflow error, and a real number that cannot be stored exactly gives a roundoff error. Using more bits is the usual way to reduce either problem.
Key words
Overflow: a result that is too big to fit in the bits set aside for it.
Roundoff error: a small error because a number cannot be stored exactly in a fixed number of bits.
Fixed number of bits: every number gets the same amount of space, no matter how large it is.