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

前端 未结 3 1105
清酒与你
清酒与你 2020-11-29 07:53

It seems to me that the code

console.log(1 / 0)

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

3条回答
  •  天涯浪人
    2020-11-29 08:41

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

    • http://en.wikipedia.org/wiki/Floating-point#Infinities
    • http://en.wikipedia.org/wiki/NaN#Creation

    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.

提交回复
热议问题