Emmanuel Akinfulubi

JavaScriptMath

What Number.EPSILON actually is in JavaScript

Advanced · 4 min read

Everyone learns sooner or later that 0.1 + 0.2 === 0.3 is false in JavaScript. The usual advice is "compare with Number.EPSILON instead". That advice is right for some numbers and quietly wrong for others, and the difference shows up in real code that works with prices, distances or sensor readings.

In this tutorial you will find out what Number.EPSILON actually is, then write three ways to compare decimal numbers: an absolute test, a relative test and a fixed-tolerance test. You will see each one pass and fail on purpose. You need to know Math.abs, Math.max and arrow functions.

Why decimals miss

JavaScript stores numbers in binary, in a format called a double (64 bits). Most decimals, like 0.1, have no exact binary form, the same way 1/3 has no exact decimal form. So each one is stored as the nearest double, a tiny bit off. Add two of them and the tiny errors can add up.

Doubles are spaced out

There are only so many doubles, and they are not evenly spaced. Near 1 they sit very close together. Near a billion they are much further apart. Think of the marks on a ruler that get wider the further you go. So the size of the possible error depends on the size of the number.

Here is the exercise. If you'd like to try it first, go ahead. Otherwise, read on and we will build it together.

86 Number.EPSILON, and three ways to compare floats

hard

Find out what Number.EPSILON actually is, then write an absolute comparison, a relative one and a fixed-tolerance one. Show a sum where 0.1 + 0.2 misses 0.3, a sum where the same two additions land exactly right, and a pair of values a billion times larger where the absolute test stops working.

Stuck? Three hints, smallest first
  1. 1. Number.EPSILON is the gap between 1 and the next double, which is 2 ** -52.
  2. 2. That gap grows with the magnitude of the numbers, so a fixed threshold cannot be right everywhere.
  3. 3. Scale the tolerance by the larger operand and the same test works at both sizes.

Step 1: what Number.EPSILON is

Step 1

console.log(Number.EPSILON);                  // 2.220446049250313e-16
console.log(Number.EPSILON === 2 ** -52);     // true
console.log(1 + Number.EPSILON / 2 === 1);    // true
console.log(1 + Number.EPSILON === 1);        // false

Number.EPSILON is one specific number: the gap between 1 and the next double above it. Adding half the gap to 1 rounds straight back to 1. Adding the whole gap gives a different number. So it is the right tolerance for numbers near 1, and only for those.

Step 2: the famous miss, and a sum that doesn't miss

Step 2

console.log(0.1 + 0.2);                    // 0.30000000000000004
console.log(Math.abs(0.1 + 0.2 - 0.3));    // 5.551115123125783e-17
console.log(4500 + 0.1 + 0.2 === 4500.3);  // true

0.1 + 0.2 lands one double above 0.3. But it is not true that decimal sums are always wrong: 4500 + 0.1 + 0.2 lands exactly on the double for 4500.3. Whether a sum comes out exact depends on the numbers, so check it rather than assume.

Step 3: the absolute test

The first idea: call two numbers equal if they differ by no more than Number.EPSILON.

Step 3

const closeAbsolute = (a, b) => Math.abs(a - b) <= Number.EPSILON;
console.log(closeAbsolute(0.1 + 0.2, 0.3)); // true

It works here, because 0.3 is close to 1.

Step 4: the same sum, a billion times bigger

Step 4

const scaledA = (0.1 + 0.2) * 1e9;
const scaledB = 1e9 * 0.3;
console.log(scaledA, scaledB);           // 300000000.00000006 300000000
console.log(Math.abs(scaledA - scaledB)); // 5.960464477539063e-8
console.log(closeAbsolute(scaledA, scaledB)); // false

Same arithmetic, bigger numbers. The gap is now about 6e-8, hundreds of millions of times bigger than Number.EPSILON. Near 300 million, neighboring doubles are about 6e-8 apart, so two different numbers there can never be within Number.EPSILON of each other. The absolute test can never pass. This is the wrong turn the usual advice leads to.

Step 5: the relative test

The fix is to scale the tolerance with the numbers: multiply Number.EPSILON by the bigger of the two sizes.

Step 5

const closeRelative = (a, b) => Math.abs(a - b) <= Number.EPSILON * Math.max(Math.abs(a), Math.abs(b));
console.log(closeRelative(0.1 + 0.2, 0.3), closeRelative(scaledA, scaledB)); // true true
console.log(closeRelative(0, 0), closeRelative(0, Number.MIN_VALUE));        // true false

Now both sizes pass. It still says 0 equals 0. It says 0 is not equal to Number.MIN_VALUE, the smallest positive double, because the allowed gap shrinks to almost nothing near zero. Nothing is lost when you start from exactly 0, so that is fair.

Step 6: a fixed tolerance fails the other way

Another common habit is a fixed small number such as 1e-9.

Step 6

const closeFixed = (a, b) => Math.abs(a - b) < 1e-9;
console.log(closeFixed(1e-10, 2e-10));  // true
console.log(closeFixed(1e12, 1e12 + 1)); // false

For tiny values it is far too loose: 1e-10 and 2e-10 differ by a factor of two, yet it calls them equal. For huge values it is too strict. It only suits numbers of everyday size.

Step 7: round to the places you need

Often the simplest answer is to decide how many decimal places matter, and round to that.

Step 7

console.log(Number((0.1 + 0.2).toFixed(10)) === 0.3); // true

toFixed(10) rounds to 10 places and returns text, and Number turns it back into a number.

The full solution

The answer panel has the three comparison functions and the scaled pair, and prints Number.EPSILON, both sums, every test at both sizes and the rounded version. Press Run to see the output.

The answer

const closeAbsolute = (a, b) => Math.abs(a - b) <= Number.EPSILON;
const closeRelative = (a, b) => Math.abs(a - b) <= Number.EPSILON * Math.max(Math.abs(a), Math.abs(b));
const closeFixed = (a, b) => Math.abs(a - b) < 1e-9;

// The same arithmetic, a billion times larger.
const scaledA = (0.1 + 0.2) * 1e9;
const scaledB = 1e9 * 0.3;

console.log('EPSILON            :', Number.EPSILON, 'is 2 ** -52:', Number.EPSILON === 2 ** -52);
console.log('0.1 + 0.2          :', 0.1 + 0.2, 'off by', Math.abs(0.1 + 0.2 - 0.3));
console.log('4500 + 0.1 + 0.2   :', 4500 + 0.1 + 0.2, 'exactly 4500.3?', 4500 + 0.1 + 0.2 === 4500.3);
console.log('1 + EPSILON / 2    :', 1 + Number.EPSILON / 2, 'still 1?', 1 + Number.EPSILON / 2 === 1);
console.log('scaled pair        :', scaledA, scaledB, 'gap', Math.abs(scaledA - scaledB));
console.log('absolute test      :', closeAbsolute(0.1 + 0.2, 0.3), closeAbsolute(scaledA, scaledB));
console.log('relative test      :', closeRelative(0.1 + 0.2, 0.3), closeRelative(scaledA, scaledB));
console.log('fixed 1e-9 test    :', closeFixed(1e-10, 2e-10), '<- calls these equal');
console.log('round then compare :', Number((0.1 + 0.2).toFixed(10)));

Output


      
9 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 9 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

9 checks

test('Number.EPSILON is 2 ** -52, the distance from 1 to the next double', [Number.EPSILON, Number.EPSILON === 2 ** -52], [2.220446049250313e-16, true]);
test('0.1 + 0.2 lands one step above 0.3, missing by 5.55e-17', [0.1 + 0.2, Math.abs(0.1 + 0.2 - 0.3)], [0.30000000000000004, 5.551115123125783e-17]);
test('float error is not universal: 4500 + 0.1 + 0.2 is exactly 4500.3', 4500 + 0.1 + 0.2 === 4500.3, true);
test('epsilon is the gap at 1, so half of it disappears and all of it does not', [1 + Number.EPSILON / 2 === 1, 1 + Number.EPSILON === 1], [true, false]);
test('a billion times larger, the same error is 5.96e-8 and dwarfs epsilon', [Math.abs(scaledA - scaledB), closeAbsolute(scaledA, scaledB)], [5.960464477539063e-8, false]);
test('the relative test passes at both magnitudes, which is the point of it', [closeRelative(0.1 + 0.2, 0.3), closeRelative(scaledA, scaledB)], [true, true]);
test('it still says zero equals zero, and that zero is not the smallest double', [closeRelative(0, 0), closeRelative(0, Number.MIN_VALUE)], [true, false]);
test('a fixed 1e-9 tolerance fails the other way, calling 1e-10 and 2e-10 equal', [closeFixed(1e-10, 2e-10), closeFixed(1e12, 1e12 + 1)], [true, false]);
test('rounding both sides to a stated number of places is the pragmatic answer', [Number((0.1 + 0.2).toFixed(10)), Number((0.1 + 0.2).toFixed(10)) === 0.3], [0.3, true]);

Number.EPSILON is not "the smallest error a double can have". It is one specific number, 2 ** -52, the gap between 1 and the next representable double. Near 1 it is exactly the right tolerance. Near a billion the gap between neighbouring doubles is about 1.2e-7, so Math.abs(a - b) <= Number.EPSILON is asking for something the format cannot deliver, and the test always fails.

Scaling the tolerance by Math.max(Math.abs(a), Math.abs(b)) fixes that, and it keeps the one case a purely relative test would break on: two zeros, where both sides are 0 and the comparison holds. A fixed tolerance like 1e-9 is the mirror-image mistake — fine at everyday sizes, far too loose for values around 1e-10, far too tight for values around 1e12.

The middle assertion is there as a corrective. It is easy to come away from 0.1 + 0.2 !== 0.3 believing that decimal arithmetic in doubles is always wrong. It is not: 4500 + 0.1 + 0.2 === 4500.3 is true, because at that magnitude the rounding happens to land on the same double the literal 4500.3 denotes. Whether a given expression is exact is a fact about that expression, so run it before you claim it.

Common mistakes

  • Treating Number.EPSILON as a universal tolerance. It is the gap near 1. For bigger numbers, scale it by their size.
  • Picking a fixed tolerance like 1e-9 for every size. It is too loose for tiny values and too strict for huge ones.
  • Assuming every decimal sum is wrong. 4500 + 0.1 + 0.2 === 4500.3 is true. Check the actual expression.

Try it yourself

  1. Use closeRelative to check that 0.1 * 3 is close to 0.3. Then check whether 0.1 * 3 === 0.3.
  2. Write closeTo(a, b, places) that rounds both numbers to places decimals and compares them.
  3. Check 1e9 + 5e-8 === 1e9 and 1e9 + 1e-7 === 1e9. What do the answers tell you about how far apart doubles are near a billion?

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.