本文介绍了在JavaScript中,为什么零除以零返回NaN,而其他除以零则返回Infinity?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,代码

  console.log(1/0)

$ b $ p $应该返回 NaN ,但是它会返回 Infinity 。然而这个代码:

  console.log(0/0)

返回 NaN 。有人能帮我理解这个功能的推理吗?这不仅似乎不一致,而且在 x / 0 其中 x!== 0






粗略的说,当x倾向于零时,你可以把1/0看作1 / x的极限。而且0/0根本就没有合理的解释,所以NaN。

It seems to me that the code

console.log(1 / 0)

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

console.log(0 / 0)

does return 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 / 0 where x !== 0

解决方案

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

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

这篇关于在JavaScript中,为什么零除以零返回NaN,而其他除以零则返回Infinity?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:18