[英]When does a floating point number overflow?
Why is 为什么是
Number.MAX_VALUE + Number.MAX_VALUE == Number.MAX_VALUE
=> false
but 但
Number.MAX_VALUE + Number.MIN_VALUE == Number.MAX_VALUE
=> true
? ?
My expectation was, that everything I add to the maximum value must overflow. 我的期望是,我添加到最大值的所有内容都必须溢出。
Imagine it like this: 像这样想象:
MAX_VALUE = Infinite MAX_VALUE =无限
MIN_VALUE = 0 MIN_VALUE = 0
So, when you add MAX_VALUE with MIN_VALUE it's like you added 0. So it's still MAX_VALUE. 因此,当您将MAX_VALUE与MIN_VALUE相加时,就像您添加了0。因此仍为MAX_VALUE。
Think of it as all the non-zero digits being far, far to the right, and therefore they have no net effect.
可以将其视为所有非零数字都在很远的右边,因此它们没有净作用。
JavaScript's Number.MAX_VALUE
is misnamed. JavaScript的
Number.MAX_VALUE
被错误命名。 It is not the maximum representable value. 它不是最大可表示值。 It is the maximum representable finite value.
它是最大可表示的有限值。 The actual maximum representable value is infinity.
实际最大可表示值是无穷大。
If the name were correct, so that Number.MAX_VALUE
were infinity, then Number.MAX_VALUE + Number.MAX_VALUE == Number.MAX_VALUE
would be true, because infinity plus infinity is true. 如果名称正确,则
Number.MAX_VALUE
为无穷大,则Number.MAX_VALUE + Number.MAX_VALUE == Number.MAX_VALUE
将为true,因为无穷大加无穷Number.MAX_VALUE + Number.MAX_VALUE == Number.MAX_VALUE
真。
As it is the mathematical value of Number.MAX_VALUE + Number.MAX_VALUE
is beyond the representable finite values, so it is rounded to infinity. 由于
Number.MAX_VALUE + Number.MAX_VALUE
的数学值超出了可表示的有限值,因此将其舍入为无穷大。
Number.MAX_VALUE + Number.MIN_VALUE == Number.MAX_VALUE
is true because arithmetic rounds to the nearest representable value, and adding a small value to the maximum finite value produces a result very close to the maximum finite value, so it rounds to the maximum finite value. Number.MAX_VALUE + Number.MIN_VALUE == Number.MAX_VALUE
为true,因为算术四舍五入到最接近的可表示值,并且将一个小值加到最大有限值会产生非常接近最大有限值的结果,因此将其四舍五入到最大值有限值。
(Note: The rule about rounding to the nearest representable value treats infinity as if it were in a normal place at the end of the finite values, so any result that is greater than or equal to Number.MAX_VALUE
plus half a “step” beyond it rounds to infinity.) (注意:关于四舍五入到最接近的可表示值的规则将无穷大当做它在有限值末尾的正常位置,因此,任何大于或等于
Number.MAX_VALUE
加上超出一半的“步长”会四舍五入为无穷大。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.