Ask a computer a very simple question, and you may get a very strange answer.
What is 0.1 + 0.2?
On paper, that's easy. One tenth plus two tenths is three tenths: 0.3. Your phone's calculator app will say 0.3.
But type 0.1 + 0.2 into many programming languages (Python and JavaScript, for example), and this is what you see:
0.1 + 0.2 = 0.30000000000000004
Whoa. Where did that extra 4 at the end come from? Is the computer broken?
Stop and Think
Hint 1: Think about 1 ÷ 3. How do you write it as a decimal? Can you write it exactly?
Hint 2: Computers don't write numbers using tens. They use only 0s and 1s, which means they count in twos.
Hint 3: What if a number that is neat in tens is messy in twos?
A Warm-Up With Thirds
Here's a problem that happens in ordinary decimals. One third is:
1 ÷ 3 = 0.3333333... (the 3s never stop)
You can never write one third exactly as a decimal. Suppose you only have room for three decimal places. Then you'd have to store it as 0.333. Now add three of those:
0.333 + 0.333 + 0.333 = 0.999
But three thirds is really 1! The tiny gap comes from squeezing a never-ending decimal into a small space. (If that reminds you of 0.999... = 1, it should.)
The Same Problem in Binary
Computers store numbers in binary, a system with only 0s and 1s. In binary, the places are worth halves, quarters, eighths, sixteenths and so on, not tenths and hundredths.
Some numbers are neat in binary:
0.5 is one half. Neat!
0.25 is one quarter. Neat!
0.75 is half plus a quarter. Neat!
But one tenth is a mess. Written in binary, 0.1 never stops:
0.1 (in tens) = 0.0001100110011001100110011... (in twos, repeating for ever)
It's like one third in decimals. A computer has room for only about 53 binary digits, so it has to cut the number off and store the nearest value it can. That nearest value is very close to 0.1, but not exactly 0.1:
What the computer stores for 0.1: 0.1000000000000000055511151231257827...
So Where Does the 4 Come From?
The same thing happens to 0.2 and to 0.3. Each is stored as the nearest binary value:
0.1 is stored as 0.100000000000000005551...
0.2 is stored as 0.200000000000000011102...
0.3 is stored as 0.299999999999999988897...
Now add the stored 0.1 and the stored 0.2. You get a number a tiny bit bigger than 0.3, and after rounding to the nearest value the computer can hold, it comes out as:
0.3000000000000000444...
That is not the same stored number as 0.3, which is 0.29999999999999998889... They are about 0.00000000000000006 apart. When the computer shows the result, you see 0.30000000000000004.
So it isn't a mistake in the adding. It's a tiny error that came from storing 0.1 and 0.2 in the first place. (We checked these stored values with a computer. Python's own tutorial shows the same stored value for 0.1.)
More Strange Sums
The same thing happens with other everyday numbers:
0.1 + 0.7 = 0.7999999999999999
0.3 − 0.1 = 0.19999999999999998
1.1 + 2.2 = 3.3000000000000003
But sums with halves, quarters and eighths are exact:
0.5 + 0.25 = 0.75 (exactly right)
What Do Programmers Do?
They don't panic. They have two common fixes:
Compare with a tolerance. Instead of asking "is the answer exactly 0.3?", they ask "is it close enough to 0.3?"
Count in whole units. For money, they store amounts in whole cents. So 10 cents + 20 cents = 30 cents, with no decimals at all.
Try It Yourself
Thirds again. If you only keep four decimal places, what do you get from 0.3333 + 0.3333 + 0.3333?
Neat or messy? Which of these are neat in binary (sums of halves, quarters, eighths and so on): 0.375, 0.2, 0.0625, 0.9?
Cents. Why is it safer to add 10 cents and 20 cents as the whole numbers 10 and 20?
Check your answers: 0.3333 + 0.3333 + 0.3333 = 0.9999, not 1. 0.375 is 3 eighths and 0.0625 is 1 sixteenth, so both are neat. 0.2 and 0.9 are messy, just like 0.1. And whole numbers like 10 and 20 can be stored exactly, so 10 + 20 is exactly 30.
The OddMaths Takeaway
Whenever you squeeze a never-ending number into a finite space, you lose a tiny bit. In decimals, one third does it. In binary, one tenth does it.
So the computer isn't bad at maths. It's doing exactly what it should with the numbers it can store. The trick is knowing that 0.1 is, to a computer, as awkward as 1/3 is to us.