Featurecore
Floating-point numbers
Also called IEEE 754, binary64, double precision, 0.1 + 0.2, Number.EPSILON, safe integers, MAX_SAFE_INTEGER, negative zero, -0.
A Number is an IEEE 754 binary64 value. Decimal fractions such as 0.1 are rounded, integers are exact only up to 2^53 - 1, and there is a negative zero that === cannot see.
- Language
- JavaScript
Synopsis
| Quantity | Value |
|---|---|
| format | IEEE 754-2019 binary64 |
largest finite, Number.MAX_VALUE |
about 1.8e308 |
smallest positive, Number.MIN_VALUE |
2^-1074, about 5e-324 |
largest safe integer, Number.MAX_SAFE_INTEGER |
2^53 - 1 = 9007199254740991 |
gap after 1, Number.EPSILON |
2^-52, about 2.22e-16 |
| zeros | +0 and -0 |
The Number type represents "the double-precision floating point IEEE 754-2019 binary64 values".1 The limits are from MDN.2 3
console.log(0.1 + 0.2, 0.1 + 0.2 === 0.3);
console.log(2 ** 53, 2 ** 53 + 1, Number.MAX_SAFE_INTEGER === 2 ** 53 - 1);
console.log(42 / -0, -0 === 0, Object.is(-0, 0));
0.30000000000000004 false
9007199254740992 9007199254740992 true
-Infinity true false
Semantics
r1. The values. "The Number type has exactly 18,437,736,874,454,810,627 (that is, 2 64 - 2 53 + 3 ) values".1 All the IEEE NaN values "are represented in ECMAScript as a single special NaN value". There is a positive and a negative Infinity.1
r2. Decimal fractions are rounded. MDN says that "a base-2 (binary) system cannot represent 0.1 exactly. Thus, for example, 0.1 + 0.2 is not exactly equal to 0.3".3 Python's tutorial says the same of its floats: they "are represented in computer hardware as base 2 (binary) fractions."4 Example: synopsis.
r3. Comparing with a tolerance. MDN notes that "it is often advised that floating point numbers should never be compared with === ." Number.EPSILON "is usually a reasonable threshold for errors if the arithmetic is around the magnitude of 1", but it "is inappropriate for any arithmetic operating on a larger magnitude."3 Example: epsilon.
r4. Precision falls with size. MDN notes 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."3
r5. Safe integers. The format "can only safely represent integers between -(2 53 – 1) and 2 53 – 1". "Safe" means "the ability to represent integers exactly and to compare them correctly."5 2^53 can be represented, but it is not safe, because "the integer 2 53 + 1 can't be directly represented in IEEE-754 but instead rounds to 2 53".6 Example: safe.
r6. Overflow and underflow. "Positive values greater than Number.MAX_VALUE are converted to Infinity . Positive values smaller than Number.MIN_VALUE are converted to 0 ." Negative values in the same ranges go to -Infinity and -0.2 Example: limits.
r7. Negative zero. "Note that there is both a positive zero and a negative zero ."1 "+0 === -0 is true", but "you are able to notice this when you divide by zero".2 Object.is() tells them apart. "The only difference between Object.is() and === is in their treatment of signed zeros and NaN values."7 Example: synopsis.
Examples
The examples run with Node.js 24.
Number.EPSILON works near 1 and fails near 1000 (rule r3). MDN gives these cases:3
function equal(x, y) {
return Math.abs(x - y) < Number.EPSILON;
}
console.log(equal(0.2 + 0.1, 0.3), 1000.1 + 1000.2, equal(1000.1 + 1000.2, 2000.3));
console.log(Math.abs(1000.1 + 1000.2 - 2000.3) < 1e-9);
true 2000.3000000000002 false
true
Above MAX_SAFE_INTEGER, distinct integers compare equal (rule r5):
const max = Number.MAX_SAFE_INTEGER;
console.log(max + 1 === max + 2, Number.isSafeInteger(max), Number.isSafeInteger(max + 1));
console.log(9007199254740993, BigInt(max) + 2n);
true true false
9007199254740992 9007199254740993n
The literal 9007199254740993 rounds to the nearest double, 9007199254740992. A BigInt keeps the exact integer.
Overflow, underflow and a negative zero from a product (rules r6 and r7):
console.log(Number.MAX_VALUE * 2, Number.MIN_VALUE / 2, -Number.MIN_VALUE / 2);
console.log(Object.is(-Number.MIN_VALUE / 2, -0), -1 * 0, Math.round(-0.4), String(-0));
Infinity 0 -0
true -0 -0 0
Pitfalls
0.1 + 0.2 !== 0.3
Do not test fractions with === (rule r3). Compare with a tolerance that fits the size of the values.
Integer IDs above 2^53
A 64-bit ID from a server can be larger than MAX_SAFE_INTEGER. As a Number, two IDs can become equal (rule r5). Keep such IDs as strings or BigInts.
-0 prints as 0 in a string
String(-0) is '0' (limits), and -0 === 0 is true. Use Object.is(x, -0) to find a negative zero (rule r7).
Comparison
| Topic | JavaScript | Python |
|---|---|---|
| float format | binary641 | base 2 fractions in hardware4 |
0.1 + 0.2 |
0.30000000000000004 (synopsis) |
not equal to 0.34 |
| close-enough test | Math.abs(a - b) < tol3 |
math.isclose(), which "can be useful for comparing inexact values"4 |
| integers | the same type, exact to 2^53 - 15 | a separate int type |
See Floating-point arithmetic: issues and limitations and Floating-point rounding error.
References
- ECMAScript 2026 Language Specification, 6.1.6.1 The Number Type
- JavaScript data types and data structures, MDN, Number type
- Number.EPSILON, MDN, Value; Description; Testing equality
- Floating-Point Arithmetic, Issues and Limitations, The Python Tutorial, introduction
- Number.MAX_SAFE_INTEGER, MDN, Value; Description
- Number.isSafeInteger(), MDN, Description
- Object.is(), MDN, Description
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag P0XYWR Edit this page History Report an error Markdown