Skip to content
Velloreth
Programming 4 min read

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

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:

PartBits
Sign1
Exponent11
Fraction52

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:

OptionExampleNotes
Integer cents1999exact, simple
A decimal type19.99exact, 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.

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.

Frequently asked questions

Why is 0.1 + 0.2 not 0.3?
Because 0.1 and 0.2 cannot be represented exactly in binary floating point. They are stored as the nearest representable value, and the sum of those approximations is 0.30000000000000004. The error is tiny and it is real, and it accumulates over many operations.
How do I compare two floats?
Compare the absolute difference to a small tolerance, such as 1e-9, rather than using ==. The tolerance depends on the magnitude of the values, so a relative comparison is often better. For money, do not use a float at all.
How should I store money?
As an integer number of the smallest unit, such as cents, or as a decimal type. An integer is exact and simple, and a decimal type is exact and readable. A float is neither, and a rounding error in a balance is a real bug.
What is the difference between float and double?
A float is 32 bits with about 7 significant decimal digits, and a double is 64 bits with about 15. A double is the default in most languages. Neither is exact for a decimal fraction.
What is NaN?
Not a Number, the result of an invalid operation such as 0/0 or the square root of a negative. NaN is not equal to anything, including itself, so a comparison with NaN is always false. Use Number.isNaN to test for it.
What is the safe integer range in JavaScript?
Number.MAX_SAFE_INTEGER is 2^53 - 1, which is 9007199254740991. Above that, two integers can map to the same double. Use BigInt for a larger integer, and be aware that a JSON number above the range loses precision.