The answer to this question may be painfully obvious but I can’t find it in the Mozilla docs nor on Google from a cursory search.
If you have some code like this
Number.MAX_VALUE + 1; // Infinity, right?
Number.MIN_VALUE - 1; // -Infinity, right?
Then I would expect adding anything to Number.MAX_VALUE would push it over to Infinity. The result is just Number.MAX_VALUE spat right back at me.
However, when playing around in the Chrome JS console, I noticed that it didn’t actually become Infinity until I added/subtracted enough:
Number.MAX_VALUE + Math.pow(100,1000); // now we hit Infinity
Number.MIN_VALUE - Math.pow(100,1000); // -Infinity at last
What is the explanation for this “buffer” between Number.MAX_VALUE and Infinity?
Standardwise…
In ECMAScript, addition of two nonzero finite numbers is implemented as (ECMA-262 §11.6.3 "Applying the Additive Operators to Numbers"):
IEEE-754’s round-to-nearest mode specifies that (IEEE-754 2008 §4.3.1 "Rounding-direction attributes to nearest")
ECMAScript does not specify which of the round-to-nearest, but it doesn’t matter here because both gives the same result. The number in ECMAScript is "double", in which
so the result must be at least 21024 – 2970 ~ 1.7976931348623158 × 10308 in order to round to infinity. Otherwise it will just round to MAX_VALUE, because that is the closer than Infinity.
Notice that MAX_VALUE = 21024 – 2971, so you need to add at least 2971 – 2970 = 2970 ~ 9.979202 × 10291 in order to get infinity. We could check:
Meanwhile, your
Math.pow(100,1000)~ 26643.9 is well beyond 21024 – 2970. It is already infinity.