Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Number and BigInt

Number.EPSILON

Reference for Number.EPSILON: the gap between 1 and the next double, why 0.1 + 0.2 !== 0.3, and how to compare floating-point numbers with a tolerance.

Number.EPSILON is the difference between 1 and the next larger number that a 64-bit floating-point value can represent. Its value is 2^-52, about 2.220446049250313e-16.

People use it as a tolerance when comparing results of decimal arithmetic, because numbers such as 0.1 and 0.2 cannot be stored exactly in binary, so 0.1 + 0.2 is not exactly 0.3.

JavaScript
Number.EPSILON

Try it

JS Why 0.1 + 0.2 is not 0.3

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS A tolerance that scales

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
Number.EPSILON
Part Description
Value 2 ** -52, about 2.220446049250313e-16.
Attributes Not writable, not enumerable, not configurable.

Usage notes

  • It is a spacing, not a universal error bound. The gap between neighbouring doubles grows with magnitude: near 1e10 it is far larger than EPSILON. For numbers much bigger than 1, scale the tolerance by the size of the operands.
  • For numbers much smaller than 1, EPSILON is too coarse: 1e-20 and 5e-20 differ by less than EPSILON yet are very different values. Compare relative error there.
  • Prefer integers for money. Store cents as integers, or use BigInt, and avoid comparing money with a tolerance.
  • Rounding for display is a different task: use toFixed() or Intl.NumberFormat.
  • It is exactly representable: 1 + Number.EPSILON !== 1, but 1 + Number.EPSILON / 2 === 1.
  • Do not use it with NaN or Infinity. Any comparison with NaN is false; Infinity - Infinity is NaN.
  • Pick the tolerance for the problem. Sums of many values accumulate more error, so 4 * Number.EPSILON or more is normal for real calculations.

Browser support

Supported in all current browsers; it arrived with ES2015. Internet Explorer lacks it.