Adding to Number.MAX_VALUE

Standardwise…

In ECMAScript, addition of two nonzero finite numbers is implemented as (ECMA-262 §11.6.3 “Applying the Additive Operators to Numbers”):

the sum is computed and rounded to the nearest representable value using IEEE 754 round-to-nearest mode. If the magnitude is too large to represent, the operation overflows and the result is then an infinity of appropriate sign.

IEEE-754’s round-to-nearest mode specifies that (IEEE-754 2008 §4.3.1 “Rounding-direction attributes to nearest”)

In the following two rounding-direction attributes, an infinitely precise result with magnitude at least bemax ( b − ½ b1-p ) shall round to ∞ with no change in sign; here emax and p are determined by the destination format (see 3.3). With:

  • roundTiesToEven, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with an even least significant digit shall be delivered
  • roundTiesToAway, the floating-point number nearest to the infinitely precise result shall be delivered; if the two nearest floating-point numbers bracketing an unrepresentable infinitely precise result are equally near, the one with larger magnitude shall be delivered.

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

  • b = 2
  • emax = 1023
  • p = 53,

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:

>>> Number.MAX_VALUE + 9.979201e291
1.7976931348623157e+308
>>> Number.MAX_VALUE + 9.979202e291
Infinity

Meanwhile, your Math.pow(100,1000) ~ 26643.9 is well beyond 21024 – 2970. It is already infinity.

Leave a Comment