Every language says 0.30000000000000004. Here is why binary can't store tenths, what it costs, and how to dodge it.
Open any Python shell, type 0.1 + 0.2, and it prints 0.30000000000000004. Not 0.3.
Every language does this, and it isn't a bug. It's a side effect of how computers store numbers, and once you see it, you'll never trust == on decimals again.
One third can't be written exactly in decimals. Try it: you get 0.333... and it never ends. You can only write a few threes and round it.
Computers have the same kind of problem with 0.1. They count in binary (only 0s and 1s), so they build numbers from halves, quarters, eighths and so on, and one tenth can't be made exactly from those.
So 0.1 is the "1/3" of binary. Goldberg's classic paper says it plainly: 0.1 "has an infinite repeating representation" in binary.
The computer has to cut the pattern somewhere, so it stores the closest value it can.
The chop is the step where the tiny error sneaks in. But what exactly gets stored?
Most languages use the IEEE 754 double. It's a standard that squeezes each number into 64 bits. The number part (the "precision") gets 53 bits.
Python's docs show that 0.1 is stored as the fraction 3602879701896397 / 2 ** 55. That is close to 1/10 but not equal. The exact stored value is:
0.1000000000000000055511151231257827021181583404541015625
So the computer never sees 0.1. It sees a number a tiny bit bigger. What happens when we add two of those?
Here are the exact stored values, as Python shows them. 0.2 is also a tiny bit too big, and the sum lands just above 0.3 after rounding.
| Number | Exact value stored |
|---|---|
| 0.1 | 0.1000000000000000055511151231257827021181583404541015625 |
| 0.2 | 0.200000000000000011102230246251565404236316680908203125 |
| 0.1 + 0.2 | 0.3000000000000000444089209850062616169452667236328125 |
Click Next to follow the numbers one at a time.
0.1 is stored a tiny bit too big
In the last step, the answer isn't random. It's a neighbour of 0.3, not 0.3 itself. That's why 0.1 + 0.2 == 0.3 is False.
It's a tiny error. But does it ever matter?
Mostly it's a 17th-digit problem, so a game score won't care. But small errors can add up. Goldberg's paper has a money example: working out compound interest with sloppy rounding gave 37614.05. That's $1.40 off.
And equality checks go wrong. A loop that waits for a value to be exactly 0.3 can wait forever, because the value never lands exactly there.
So what do we do about it? Two fixes.
Integers are exact (up to 2^53 - 1 in JavaScript, per MDN). So don't store 10.50. Store 1050 cents, and only add the decimal point when you show it.
price_cents = 1050
total = price_cents * 3 # 3150, exact
print(total / 100) # 31.5, only for display
If you need real decimal maths, Python's docs point to the decimal module, "suitable for accounting applications".
For things that really are fractions (like sensor readings), never ask "are these equal?". Ask "are these close enough?".
import math
math.isclose(0.1 + 0.1 + 0.1, 0.3) # True
Goldberg warns this isn't a cure-all, because you still have to choose how close is "close". Pick it based on what you're measuring.
==.No. It's doing the best it can with 53 bits. The mismatch comes from expecting binary to store decimal fractions exactly.