Javascript 是否处理整数上溢和下溢?如果是,如何?
我们知道 Java 不处理下溢和上溢,但是 Javascript 如何处理整数?
We know that Java does not handle underflows and overflows, but how does Javascript handle these for integers?
它会回到最小值/最大值吗?如果是,是哪个最小值/最大值?
Does it go back to a minimum/maximum? If yes, which minimum/maximum?
我需要拆分一个字符串并根据其字符计算一个哈希值.
I need to split a string and compute a hash value based on its characters.
推荐答案
在一个简单的测试中,当我尝试这个时:
In a simple test, when I try this:
var max = Number.MAX_VALUE;
var x = max + 10;
var min = Number.MIN_VALUE;
var y = min / 10;
我发现 x
和 max
具有相同的值(在 Chrome、IE 和 Firefox 中),所以看起来一些溢出只是与最大值挂钩.而且,y
与 0
挂钩,因此一些下溢似乎归零.
I find that x
and max
have the same value (in Chrome, IE and Firefox) so it appears that some overflows are just pegged to the max value. And, y
gets pegged to 0
so some underflows seem to go to zero.
啊,但事情没那么简单.并非所有上溢都转到 Number.MAX_VALUE
,也不是所有下溢都转到 Number.MIN_VALUE
.如果你这样做:
Ahhh, but it is not quite that simple. Not all overflows go to Number.MAX_VALUE
and not all underflows go to Number.MIN_VALUE
. If you do this:
var max = Number.MAX_VALUE;
var z = max * 2;
那么,z
将是 Infinity
.
事实证明,这取决于您上溢/下溢的程度.如果你走得太远,你会得到 INFINITY.这是因为使用了 IEEE 754 舍入到最近的模式,其中最大值可以被认为比无穷大更接近.有关详细信息,请参阅添加到 Number.MAX_VALUE.根据该答案,将 1.7976931348623158 × 10308 或更大的值四舍五入到无穷大.Number.MAX_VALUE 之间的值将四舍五入为 Number.MAX_VALUE.
It turns out that it depends upon how far you overflow/underflow. If you go too far, you will get INFINITY instead. This is because of the use of IEEE 754 round-to-nearest mode where the max value can be considered nearer than infinity. See Adding to Number.MAX_VALUE for more detail. Per that answer, values of 1.7976931348623158 × 10308 or greater round to infinity. Values between Number.MAX_VALUE and that will round to Number.MAX_VALUE.
为了让事情变得更复杂,还有一些 Javascript 支持的渐进下溢.这是浮点值的尾数有前导零的地方.逐渐下溢允许浮点表示一些较小的数字,如果没有它就无法表示,但它们的表示精度会降低.
To, make things even more complicated, there is also something as gradual underflow which Javascript supports. This is where the mantissa of the floating point value has leading zeroes in it. Gradual underflow allows floating point to represent some smaller numbers that it could not represent without that, but they are represented at a reduced precision.
您可以确切地看到限制在哪里:
You can see exactly where the limits are:
>>> Number.MAX_VALUE + 9.979201e291
1.7976931348623157e+308
>>> Number.MAX_VALUE + 9.979202e291
Infinity
这是一个可在任何浏览器中尝试的可运行代码段:
Here's a runnable snippet you can try in any browser:
var max = Number.MAX_VALUE;
var x = max + 10;
var min = Number.MIN_VALUE;
var y = min / 10;
var z = max * 2;
document.getElementById("max").innerHTML = max;
document.getElementById("max10").innerHTML = x;
document.getElementById("min").innerHTML = min;
document.getElementById("min10").innerHTML = y;
document.getElementById("times2").innerHTML = z;
body {
font-family: "Courier New";
white-space:nowrap;
}
Number.MAX_VALUE = <span id="max"></span><br>
Number.MAX_VALUE + 10 = <span id="max10"></span><br>
<br>
Number.MIN_VALUE = <span id="min"></span><br>
Number.MIN_VALUE / 10 = <span id="min10"></span><br>
<br>
Number.MAX_VALUE * 2 = <span id="times2"></span><br>
相关文章