0.1 + 0.2 is not 0.3
Type this into almost any programming language:
>>> 0.1 + 0.2 == 0.3
False
>>> 0.1 + 0.2
0.30000000000000004It is not a bug, and it is not specific to Python. JavaScript, Java, C, Go and Rust all give the same answer, because they all store these numbers the same way. The cause is simple once you see it: 0.1 cannot be written down exactly in binary.
Some fractions never end
You already know this problem in decimal. Write one third as a decimal:
0.3333333...
It never ends. Any amount of paper leaves you slightly short. The reason is that 3 does not divide evenly into a power of 10.
Computers store numbers in binary, base 2. In binary, a fraction only comes out exactly if it can be built from halves, quarters, eighths and so on. One half is 0.1. Three quarters is 0.11. But one tenth is not a sum of those, however many you use.
Start. To write a fraction in binary: double it. If the result reaches 1, write a 1 and keep what is left over. Otherwise write a 0. Repeat with the leftover.
- Left over
- 0.1
- Bits written
- 0
The pattern 0011 repeats for ever, just as the 3s do in one third.
So the computer rounds
A standard floating-point number (a "double") has 64 bits, of which 53 hold the significant digits. The endless binary fraction is cut off there and rounded to the nearest value that fits.
So when you write 0.1, what is stored is not one tenth. It is the closest number the format can represent, which is very slightly more:
0.1000000000000000055511151231257827...
The error is tiny, about one part in a hundred million billion. Usually it is invisible. It shows up when two rounded numbers are combined and the result is compared with a third rounded number.
1a = 0.12b = 0.23total = a + b4print(total == 0.3)5print(total)
What is really stored
nothing yet
Start. Press play, or step through one line at a time.
0.1 was stored a hair too high. 0.2 was stored a hair too high. Their sum landed a hair above 0.3. And the 0.3 you typed was stored a hair below 0.3. Two different numbers, so == says false.
Which numbers are exact?
| Number | Exact in binary? | Why |
|---|---|---|
| 0.5, 0.25, 0.125 | Yes | Halves, quarters, eighths |
| 0.75 | Yes | A half plus a quarter |
| 0.1, 0.2, 0.3, 0.7 | No | Tenths are not sums of halves |
| Whole numbers up to about 9 quadrillion | Yes | They fit in 53 bits |
| Larger whole numbers | Not all | Beyond 2⁵³, not every integer is available |
So 0.5 + 0.25 == 0.75 is true, and 0.1 + 0.2 == 0.3 is false.
Living with it
Never compare floats with ==
Ask whether they are close enough.
pythonimport math math.isclose(0.1 + 0.2, 0.3) # TrueNever store money as a float
Errors of a fraction of a cent add up over millions of transactions. Store whole numbers of the smallest unit (cents, paise), or use a decimal type.
pythonfrom decimal import Decimal Decimal("0.1") + Decimal("0.2") == Decimal("0.3") # TrueRound when showing, not when storing
Keep full precision inside the program and format to two decimals at the last moment.
Be careful adding many small numbers
Each addition can add a little error. Adding 0.1 ten times does not give exactly 1.0.
Key takeaways
- Computers store fractions in binary, and most decimal fractions have no exact binary form.
- 0.1 in binary repeats for ever, like one third in decimal, so it is rounded.
- 0.1 + 0.2 lands slightly above 0.3, while 0.3 itself is stored slightly below.
- Compare floats with a tolerance, never with ==.
- Use integers or a decimal type for money.