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.
Number.EPSILON
Try it
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Press Run (or Ctrl + Enter) to see the console output here.
No output.
Syntax
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
1e10it is far larger thanEPSILON. For numbers much bigger than 1, scale the tolerance by the size of the operands. - For numbers much smaller than 1,
EPSILONis too coarse:1e-20and5e-20differ by less thanEPSILONyet 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()orIntl.NumberFormat. - It is exactly representable:
1 + Number.EPSILON !== 1, but1 + Number.EPSILON / 2 === 1. - Do not use it with
NaNorInfinity. Any comparison withNaNisfalse;Infinity - InfinityisNaN. - Pick the tolerance for the problem. Sums of many values accumulate more error, so
4 * Number.EPSILONor more is normal for real calculations.
Browser support
Supported in all current browsers; it arrived with ES2015. Internet Explorer lacks it.