Why is 0.1 + 0.2 !== 0.3?
Binary fractions, the IEEE 754 standard — and how to live with them: comparing with a tolerance and keeping money in integers.
0.30000000000000004.com. Spoiler: it's not a bug. And it's not JavaScript.console.log(0.1 + 0.2);
console.log(0.1 + 0.2 === 0.3);
console.log(0.5 + 0.25 === 0.75);Back to school. Try writing 1/3 as a decimal: 0.3333… — the threes never end. At some point you have to stop, and what you wrote is slightly less than a third. Decimal notation can only write fractions exactly when the denominator is made of twos and fives (1/2, 1/4, 1/5, 1/10) — because 10 = 2 × 5.
A computer counts in binary, and two has only one factor — two itself. Only fractions like 1/2, 1/4, 1/8 and their sums are exact. And 1/10 in binary is an endless fraction 0.000110011001100…, just like our 0.333…. That's why 0.5 + 0.25 comes out exact, while 0.1 and 0.2 don't.
How many digits do we keep? The answer comes from the IEEE 754 standard (1985). Every number in JavaScript takes 64 bits:
- 1 bit for the sign;
- 11 bits for the exponent (where the point goes);
- 52 bits for the digits themselves.
That's about 15–17 significant decimal digits. The endless tail of 0.1 is cut off and rounded to the nearest number that fits in 64 bits. The stored "0.1" is a hair more than a real tenth, and so is "0.2". Add them and the tiny errors add up, so the sum lands not on the number that stores "0.3" but on its neighbour.
console.log((0.1).toFixed(20));
console.log((0.3).toFixed(20));
console.log((0.1 + 0.2).toFixed(20));Why "not JavaScript"? Because IEEE 754 is built right into the processor, and almost every language uses it: Python, Java, C, C#, Go, Rust. In Python, 0.1 + 0.2 also gives 0.30000000000000004. JavaScript just has a single number type, and it's a floating-point one, so everybody runs into this — not only people who chose a double.
And why don't processors use decimal? Binary fractions are fast and compact. For physics, graphics and games an error in the 17th digit doesn't matter — and speed matters a lot.
So how do we live with it? Rule one: never compare fractions with `===`. Compare "close enough": the difference is below a small tolerance. For everyday values 1e-9 (one billionth) works fine.
JavaScript also ships a ready-made constant, Number.EPSILON — about 2.2e-16. It's the gap between 1 and the next number a computer can store.
function almostEqual(a, b) {
return Math.abs(a - b) < 1e-9;
}
console.log(almostEqual(0.1 + 0.2, 0.3));
console.log(Number.EPSILON);
console.log(Math.abs(0.1 + 0.2 - 0.3) < Number.EPSILON);console.log(1000.1 + 1000.2);
console.log(Math.abs(1000.1 + 1000.2 - 2000.3) < Number.EPSILON);
console.log(Math.abs(1000.1 + 1000.2 - 2000.3) < 1e-9);Rule two: money goes in integers. Integers are stored exactly (all the way up to Number.MAX_SAFE_INTEGER, which is over nine quadrillion). So prices are kept in cents: 4990, not 49.9. You add and multiply integers, and divide by 100 only at the very end, to show the amount. Banks and payment systems do exactly this: in the Stripe API, for example, an amount is always given in cents.
const bread = 499; // $4.99
const milk = 289; // $2.89
const fish = 1310; // $13.10
const total = bread + milk + fish;
console.log(total);
console.log("$" + (total / 100).toFixed(2));0.1 + 0.2 !== 0.3 isn't a mistake, it's the price of speed. A computer stores fractions in binary the way we store a third as 0.333. I compare fractions with a tolerance, keep money in cents — and sleep soundly. Well, I'd sleep anyway. I'm a cat.Myth or fact?
Decide whether the statement is true. Got it wrong? Try again; once you're right, Byte explains what's going on.