问题描述
在我看来,代码
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 returnsInfinity
. 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 ofx / 0
wherex !== 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!