Bloodyaugust Bloodyaugust - 1 year ago 85
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
, but instead it returns
. However this code:

console.log(0 / 0)

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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download