All numbers in Javascript are 64bit (8 bytes) floating point numbers but why the effective range of JavaScript is 5e-324 (negative) to 1.7976931348623157e+308 (positive)?
-
From all the possible values, 9007199254740990 of them are represented as NaN (Not a number).Šime Vidas– Šime Vidas2012-04-30 15:12:35 +00:00Commented Apr 30, 2012 at 15:12
-
edwardpku, you should probably accept either MДΓΓ БДLL's answer or mine (depending on whether you find a pointer to the spec, or an explanation of why the spec says that, more helpful).Gareth McCaughan– Gareth McCaughan2012-05-02 09:27:43 +00:00Commented May 2, 2012 at 9:27
Add a comment
|
2 Answers
Because that is what is defined by the IEEE 754 spec.
0x 0000 0000 0000 0001 = 2⁻¹⁰²²⁻⁵² ≈ 4.9406564584124654 x 10⁻³²⁴ (Min subnormal positive double)
0x 000f ffff ffff ffff = 2⁻¹⁰²² - 2⁻¹⁰²²⁻⁵² ≈ 2.2250738585072009 x 10⁻³⁰⁸ (Max subnormal positive double)
0x 0010 0000 0000 0000 = 2⁻¹⁰²² ≈ 2.2250738585072014 x 10⁻³⁰⁸ (Min normal positive double)
0x 7fef ffff ffff ffff = (1 + (1 - 2⁻⁵²)) x 2¹⁰²³ ≈ 1.7976931348623157 x 10³⁰⁸ (Max Double)
Comments
Because of denormal values; see e.g. http://en.wikipedia.org/wiki/Denormal_number. These extend the range of floating-point values to allow values closer to zero than would otherwise be possible.
You would be well advised to treat these as an implementation detail whose purpose is to make calculations with very small intermediate results not behave too pathologically; when you use these very small values some precision is lost.