Floating Point Precision: Why 0.1 + 0.2 Is Not 0.3
Binary floating point cannot represent most decimals exactly. Learn the cause, the consequences, and how to handle money.
By Velloreth
The cause
A binary float stores a value as a sign, an exponent and a fraction in base 2.
A decimal fraction such as 0.1 is a repeating fraction in base 2, so it cannot be stored exactly.
The value stored is the nearest representable one.
0.1 + 0.2 // 0.30000000000000004
0.1 + 0.2 === 0.3 // false
The error is tiny and it is real, and it accumulates over many operations.
The representation
A double is 64 bits:
| Part | Bits |
|---|---|
| Sign | 1 |
| Exponent | 11 |
| Fraction | 52 |
About 15 to 17 significant decimal digits.
A float is 32 bits with about 7 significant digits.
Neither is exact for a decimal fraction.
The consequences
A comparison fails.
0.1 + 0.2 === 0.3 // false
A sum drifts.
let sum = 0;
for (let i = 0; i < 10; i++) sum += 0.1;
sum // 0.9999999999999999
A large and a small value.
1e16 + 1 === 1e16 // true, the 1 is lost
A money calculation is wrong.
19.99 * 3 // 59.97000000000001
The comparison
Never use == for two floats.
Use a tolerance.
const close = (a, b, eps = 1e-9) => Math.abs(a - b) < eps;
A relative tolerance is better for a large magnitude.
const close = (a, b, eps = 1e-9) =>
Math.abs(a - b) <= eps * Math.max(Math.abs(a), Math.abs(b));
The tolerance depends on the problem. A physics simulation and a currency calculation need different ones.
Money
Never store money in a float.
The options:
| Option | Example | Notes |
|---|---|---|
| Integer cents | 1999 | exact, simple |
| A decimal type | 19.99 | exact, readable |
| A string | "19.99" | exact, needs parsing |
An integer of the smallest unit is the common choice.
const priceCents = 1999;
const total = priceCents * 3; // 5997
A decimal type is available in Python (decimal), Java (BigDecimal), C# (decimal) and SQL (NUMERIC).
A float is neither exact nor readable, and a rounding error in a balance is a real bug.
The special values
NaN is the result of an invalid operation.
0 / 0 // NaN
Math.sqrt(-1) // NaN
NaN === NaN // false
NaN is not equal to anything, including itself. Use Number.isNaN.
Infinity is the result of an overflow or a division by zero.
1 / 0 // Infinity
-1 / 0 // -Infinity
-0 is a distinct value that compares equal to 0.
The safe integer range
JavaScript: Number.MAX_SAFE_INTEGER is 2^53 - 1.
Number.MAX_SAFE_INTEGER // 9007199254740991
9007199254740992 === 9007199254740993 // true, both round to the same double
Above the range, two integers can map to the same double.
Use BigInt for a larger integer.
A JSON number above the range loses precision, which is why a large id is often sent as a string.
The rounding
toFixed returns a string and rounds.
(0.1 + 0.2).toFixed(2) // "0.30"
Math.round rounds to the nearest integer, with a half-up rule for a positive value.
A banker’s rounding rounds a half to the nearest even, which is the default in a decimal type and is the correct choice for money.
The rule: round once, at the end, and use a decimal type or an integer.
Common mistakes
A float for money. A rounding error.
A comparison with ==. A false negative.
A sum of many small values. A drift.
A large and a small value added. The small one is lost.
A JSON number above the safe range. A precision loss.
A NaN comparison. Always false.
A toFixed used for a calculation. It returns a string.
A tolerance that is too small. A false negative.
A tolerance that is too large. A false positive.
A quotable summary
A binary float cannot represent most decimal fractions exactly, so 0.1 + 0.2 is 0.30000000000000004 and a comparison with == fails. Never compare two floats with ==, use a tolerance instead, and never store money in a float: use an integer of the smallest unit or a decimal type. NaN is not equal to anything, and a JavaScript integer above 2^53 - 1 loses precision.
Related reading
For the related topics, see binary-and-hex-explained and bitwise-operators-guide. For the broader context, date-and-time-handling and unicode-and-encoding.