Movatterモバイル変換


[0]ホーム

URL:


  1. Web
  2. JavaScript
  3. Reference
  4. Standard built-in objects
  5. Number
  6. EPSILON

Number.EPSILON

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since ⁨September 2015⁩.

TheNumber.EPSILON static data property represents the difference between 1 and the smallest floating point number greater than 1.

Try it

const result = Math.abs(0.2 - 0.3 + 0.1);console.log(result);// Expected output: 2.7755575615628914e-17console.log(result < Number.EPSILON);// Expected output: true

Value

2-52, or approximately2.2204460492503130808472633361816E-16.

Property attributes ofNumber.EPSILON
Writableno
Enumerableno
Configurableno

Description

Number.EPSILON is the difference between 1 and the next greater number representable in the Number format, becausedouble precision floating point format only has 52 bits to represent themantissa, and the lowest bit has a significance of 2-52.

Note that the absolute accuracy of floating numbers decreases as the number gets larger, because the exponent grows while the mantissa's accuracy stays the same.Number.MIN_VALUE is the smallest representable positive number, which is much smaller thanNumber.EPSILON.

BecauseEPSILON is a static property ofNumber, you always use it asNumber.EPSILON, rather than as a property of a number value.

Examples

Testing equality

Any number encoding system occupying a finite number of bits, of whatever base you choose (e.g., decimal or binary), willnecessarily be unable to represent all numbers exactly, because you are trying to represent an infinite number of points on the number line using a finite amount of memory. For example, a base-10 (decimal) system cannot represent 1/3 exactly, and a base-2 (binary) system cannot represent0.1 exactly. Thus, for example,0.1 + 0.2 is not exactly equal to0.3:

js
console.log(0.1 + 0.2); // 0.30000000000000004console.log(0.1 + 0.2 === 0.3); // false

For this reason, it is often advised thatfloating point numbers should never be compared with===. Instead, we can deem two numbers as equal if they areclose enough to each other. TheNumber.EPSILON constant is usually a reasonable threshold for errors if the arithmetic is around the magnitude of1, becauseEPSILON, in essence, specifies how accurate the number "1" is.

js
function equal(x, y) {  return Math.abs(x - y) < Number.EPSILON;}const x = 0.2;const y = 0.3;const z = 0.1;console.log(equal(x + z, y)); // true

However,Number.EPSILON is inappropriate for any arithmetic operating on a larger magnitude. If your data is on the 103 order of magnitude, the decimal part will have a much smaller accuracy thanNumber.EPSILON:

js
function equal(x, y) {  return Math.abs(x - y) < Number.EPSILON;}const x = 1000.1;const y = 1000.2;const z = 2000.3;console.log(x + y); // 2000.3000000000002; error of 10^-13 instead of 10^-16console.log(equal(x + y, z)); // false

In this case, a larger tolerance is required. As the numbers compared have a magnitude of approximately2000, a multiplier such as2000 * Number.EPSILON creates enough tolerance for this instance.

js
function equal(x, y, tolerance = Number.EPSILON) {  return Math.abs(x - y) < tolerance;}const x = 1000.1;const y = 1000.2;const z = 2000.3;console.log(equal(x + y, z, 2000 * Number.EPSILON)); // true

In addition to magnitude, it is important to consider theaccuracy of your input. For example, if the numbers are collected from a form input and the input value can only be adjusted by steps of0.1 (i.e.,<input type="number" step="0.1">), it usually makes sense to allow a much larger tolerance, such as0.01, since the data only has a precision of0.1.

Note:Important takeaway: do not simply useNumber.EPSILON as a threshold for equality testing. Use a threshold that is appropriate for the magnitude and accuracy of the numbers you are comparing.

Specifications

Specification
ECMAScript® 2026 Language Specification
# sec-number.epsilon

Browser compatibility

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp