Bloodyaugust Bloodyaugust - 1 month ago 7
Javascript Question

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

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

Answer

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.