JavaScript: Number.EPSILON and Number.MAX_SAFE_INTEGER

EPSILON

The Number.EPSILON property represents the difference between 1 and the smallest floating point number greater than 1

> Number.EPSILON == Math.pow(2, -52)
true

Use Number.EPSILON to test floating point number equality.

x = 0.2;
y = 0.3;
z = 0.1;
equal = (Math.abs(x - y + z) < Number.EPSILON);

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/EPSILON

MAX_SAFE_INTEGER & MIN_SAFE_INTEGER

The MAX_SAFE_INTEGER constant has a value of 9007199254740991 (9,007,199,254,740,991 or ~9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent integers between -(2^53 - 1) and 2^53 - 1.

The Number.MAX_SAFE_INTEGER constant represents the maximum safe integer in JavaScript (2^53 - 1). For larger integers, consider using BigInt.

Safe in this context refers to the ability to represent integers exactly and to correctly compare them. For example, Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2 will evaluate to true, which is mathematically incorrect. See Number.isSafeInteger() for more information.

> Number.MAX_SAFE_INTEGER == 9007199254740991
true

> Number.MAX_SAFE_INTEGER + 1 === Number.MAX_SAFE_INTEGER + 2
true

> 9007199254740992 === 9007199254740993
true

 

The MIN_SAFE_INTEGER constant has a value of -9007199254740991 (-9,007,199,254,740,991 or about -9 quadrillion). The reasoning behind that number is that JavaScript uses double-precision floating-point format numbers as specified in IEEE 754 and can only safely represent numbers between -(2^53 - 1) and 2^53 - 1.  

> Number.MIN_SAFE_INTEGER
-9007199254740991

> Number.MIN_SAFE_INTEGER - 1 === Number.MIN_SAFE_INTEGER - 2
true

> Number.MIN_SAFE_INTEGER - 1
-9007199254740992

> Number.MIN_SAFE_INTEGER - 2
-9007199254740992

> -9007199254740993
-9007199254740992

> Number.MIN_SAFE_INTEGER - 3
-9007199254740994

> -9007199254740992 === -9007199254740994
true

Leave a Comment

Your email address will not be published. Required fields are marked *