问题描述
这个问题的答案可能会非常明显,但我无法在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
?
:
IEEE-754 round - 到最近的模式指定
ECMAScript没有指定哪一个舍入到最近,但在这里并不重要,因为二者都给出相同的结果。 ECMAScript中的数字是double,其中
- b = 2
- emax = 1023
- p = 53,
所以结果必须至少是2 1024 1024 2 3 970 1 879×10 3 308为了四舍五入到无限。否则,它将绕过MAX_VALUE,因为比更近。
注意,MAX_VALUE = 2 - 2 971,所以你至少需要添加2 971 2 970 2 970 9.979202 ×10 以获得无限。我们可以检查: >>> Number.MAX_VALUE + 9.979201e291
1.7976931348623157e + 308
>>> Number.MAX_VALUE + 9.979202e291
Infinity
同时,你的 Math.pow(100,1000)
〜2 远远超过了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 :
IEEE-754's round-to-nearest mode specifies that
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 2 - 2 ~ 1.7976931348623158 × 10 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 = 2 - 2, so you need to add at least 2 - 2 = 2 ~ 9.979202 × 10 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)
~ 2 is well beyond 2 - 2. It is already infinity.
这篇关于添加到Number.MAX_VALUE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!