添加到Number.MAX_VALUE [英] Adding to Number.MAX_VALUE
问题描述
这个问题的答案可能会非常明显,但我无法在Mozilla文档中找到它,也无法在粗略的搜索中找到它。
如果你有一些这样的代码
Number.MAX_VALUE + 1; //无限,对不对?
Number.MIN_VALUE - 1; // -Infinity,对不对?
然后,我希望将 加入到Number.MAX_VALUE中,到 Infinity
。结果只是 Number.MAX_VALUE
吐在我身上。
Infinity
,直到我加/减足够: Number.MAX_VALUE + Math.pow(100,1000); //现在我们打Infinity
Number.MIN_VALUE - Math.pow(100,1000); // -Infinity最后
Number.MAX_VALUE
和 Infinity
?
<在ECMAScript中,添加两个非零有限数字的方式是 (ECMA-262§11.6.3Applying the添加运算符到数字) :
计算总和舍入到最接近的可表示值,使用 IEEE 754最近邻近模式。如果幅度太大而无法表示,则操作溢出,结果是无穷大的符号。
IEEE-754 round - 到最近的模式指定 (IEEE-754 2008§4.3.1舍入方向属性到最近)
在下面的两个舍入方向属性中,一个无限精确的结果,其幅度至少 b
> b - 1 p )应符合∞这里 emax 和 p 由目标格式决定(见3.3)。有:
- roundTiesToEven,最接近无限精确结果的浮点数将被传递。如果包含无法表示的无限精确的结果的两个最接近的浮点数相等,则具有最低有效数字的那个浮点数应该被传送
- roundTiesToAway是最靠近无限精确的结果应该交付;如果包含无法表示的无限精确的结果的两个最接近的浮点数同样接近,则应该交付具有较大幅度的浮点数。
ECMAScript没有指定哪一个舍入到最近,但在这里并不重要,因为二者都给出相同的结果。 ECMAScript中的数字是double,其中
- b = 2
- emax = 1023
- p = 53,
所以结果必须至少是2 1024 1024 2 3 970 1 879×10 3 308为了四舍五入到无限。否则,它将绕过MAX_VALUE,因为比
>>> Number.MAX_VALUE + 9.979201e291
1.7976931348623157e + 308
>>> Number.MAX_VALUE + 9.979202e291
Infinity
同时,你的 Math.pow(100,1000)
〜2 <6643.9 远远超过了2 1024 1024-2000 SUP> 970 。它已经是无限的。
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"):
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.
这篇关于添加到Number.MAX_VALUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!