在 JavaScript 中,为什么零除以零返回 NaN,而其他除以零返回无穷大?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18838301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:23:06  来源:igfitidea点击:

In JavaScript, why does zero divided by zero return NaN, but any other divided by zero return Infinity?

javascriptmathfloating-pointnaninfinity

提问by Bloodyaugust

It seems to me that the code

在我看来,代码

console.log(1 / 0)

should return NaN, but instead it returns Infinity. However this code:

应该返回NaN,但它返回Infinity。但是这段代码:

console.log(0 / 0)

doesreturn NaN. Can someone help me to understand the reasoning for this functionality? Not only does it seem to be inconsistent, it also seems to be wrong, in the case of x / 0where x !== 0

确实返回NaN。有人可以帮助我理解此功能的原因吗?不仅看起来不一致,而且似乎是错误的,在x / 0where的情况下x !== 0

采纳答案by Oliver Charlesworth

Because that's how floating-point is defined (more generally than just Javascript). See for example:

因为这就是定义浮点的方式(比 Javascript 更普遍)。见例如:

Crudely speaking, you could think of 1/0 as the limit of 1/x as x tends to zero (from the right). And 0/0 has no reasonable interpretation at all, hence NaN.

粗略地说,您可以将 1/0 视为 1/x 的极限,因为 x 趋于零(从右侧开始)。并且 0/0 根本没有合理的解释,因此是 NaN。

回答by Patricia Shanahan

In addition to answers based on the mathematical concept of zero, there is a special consideration for floating point numbers. Every underflow result, every non-zero number whose absolute magnitude is too small to represent as a non-zero number, is represented as zero.

除了基于零的数学概念的答案外,还有对浮点数的特殊考虑。每个下溢结果,每个绝对量值太小而不能表示为非零数的非零数,都表示为零。

0/0 may really be 1e-500/1e-600, or 1e-600/1e-500, or many other ratios of very small values.

0/0 可能真的是 1e-500/1e-600,或 1e-600/1e-500,或许多其他非常小的比率。

The actual ratio could be anything, so there is no meaningful numerical answer, and the result should be a NaN.

实际比率可以是任何值,因此没有有意义的数字答案,结果应该是 NaN。

Now consider 1/0. It does not matter whether the 0 represents 1e-500 or 1e-600. Regardless, the division would overflow and the correct result is the value used to represent overflows, Infinity.

现在考虑 1/0。0 代表 1e-500 还是 1e-600 并不重要。无论如何,除法会溢出,正确的结果是用于表示溢出的值 Infinity。

回答by Matthew Ciaramitaro

I realize this is old, but I think it's important to note that in JS there is also a -0which is different than 0or +0which makes this feature of JS much more logical than at first glance.

我意识到这是旧的,但我认为这是需要注意的重要的一点是JS也有一个-0比不同0+0使JS的这一特点远不止逻辑乍一看。

1 / 0 -> Infinity
1 / -0 -> -Infinity 

which logically makes sense since in calculus, the reason dividing by 0 is undefined is solely because the left limit goes to negative infinity and the right limit to positive infinity. Since the -0and 0are different objects in JS, it makes sense to apply the positive 0 to evaluate to positive Infinityand the negative 0 to evaluate to negative Infinity

这在逻辑上是有道理的,因为在微积分中,除以 0 的原因未定义仅仅是因为左极限为负无穷大,右极限为正无穷大。由于-00在 JS 中是不同的对象,所以应用正 0 来评估为正Infinity,应用负 0 来评估为负是有意义的Infinity

This logic does not apply to 0/0, which is indeterminate. Unlike with 1/0, we can get two results taking limits by this method with 0/0

此逻辑不适用于0/0,这是不确定的。与 with 不同1/0,我们可以通过这种方法得到两个限制结果0/0

lim h->0(0/h) = 0
lim h->0(h/0) = Infinity

which of course is inconsistent, so it results in NaN

这当然是不一致的,所以它导致 NaN