Sub topic 4
Why 0.1 + 0.2 !== 0.3 and How to Compare Floats
This chapter assumes you know basic JavaScript syntax, can run code in a browser console or Node.js, and understand ===. It builds on the earlier chapter about how JavaScript stores every Number as a 64-bit IEEE 754 double.
The surprise
console.log(0.1 + 0.2); // 0.30000000000000004
console.log(0.1 + 0.2 === 0.3); // falseThis is how binary floating point works in every language that uses IEEE 754 doubles. It is not a bug in JavaScript.
Why it happens
A double stores a number as a binary fraction. Binary can represent a fraction exactly only when its denominator is a power of 2, such as 1/2, 1/4 or 3/8. The decimal 0.1 is 1/10, and 10 has a factor of 5, so 0.1 has an infinitely repeating binary expansion. JavaScript stores the nearest double instead.
| Literal | Value actually stored (approximately) |
|---|---|
| `0.1` | 0.1000000000000000055511 |
| `0.2` | 0.2000000000000000111022 |
| `0.1 + 0.2` | 0.3000000000000000444089 |
| `0.3` | 0.2999999999999999888978 |
Each step adds a small error. The sum rounds to a different double than the literal 0.3, so strict equality fails.
Comparing with a tolerance
You should compare computed floats by checking whether they are close enough, not whether they are identical. Number.EPSILON (about 2.22e-16) is the gap between 1 and the next larger double.
function nearlyEqual(a, b, epsilon = Number.EPSILON) {
return Math.abs(a - b) < epsilon;
}
console.log(nearlyEqual(0.1 + 0.2, 0.3)); // trueCommon error: a fixed epsilon at every scale
The gap between neighbouring doubles grows as the numbers grow. Number.EPSILON is the right size only for values near 1. For larger values the absolute check fails:
const price = 1.1 * 100;
console.log(price); // 110.00000000000001
console.log(nearlyEqual(price, 110)); // falseA relative tolerance scales with the size of the inputs. Add an absolute floor as well, because a purely relative check breaks down near zero:
function closeTo(a, b, relTol = Number.EPSILON * 4, absTol = 1e-12) {
const diff = Math.abs(a - b);
const scale = Math.max(Math.abs(a), Math.abs(b));
return diff <= absTol || diff <= relTol * scale;
}
console.log(closeTo(1.1 * 100, 110)); // true
console.log(closeTo(0.1 + 0.2, 0.3)); // true
console.log(closeTo(1e-20, 0)); // true
console.log(closeTo(1, 1.001)); // falseChoose the tolerances to fit your domain. A physics simulation and a geometry test need different bounds.
Common errors at a glance
| Mistake | Fix |
|---|---|
| `a === b` on computed floats | Compare with a tolerance |
| `Number.EPSILON` for large values | Use a relative tolerance |
| Relative check alone near zero | Add an absolute floor |
| Floats for currency | Use integer cents |
Exercise
Why does 0.5 + 0.25 === 0.75 return true, while 0.1 + 0.2 === 0.3 returns false?
Show answer
0.5 (1/2), 0.25 (1/4) and 0.75 (3/4) all have power-of-2 denominators, so each one is stored exactly as a double and the sum has no rounding error. 0.1, 0.2 and 0.3 can't be represented exactly in binary, so they are stored as approximations and their errors don't cancel.
Exercise
Using the closeTo function above, will closeTo(0.7 * 3, 2.1) return true? Explain why a plain === comparison is unsafe here.
Show answer
Yes. 0.7 * 3 evaluates to 2.0999999999999996, which is within the relative tolerance of 2.1. === returns false because the product rounds to a different double than the literal 2.1.