4

Why is this so:

1 === 1;// true
0 === -0;// true
1/0 === 1/-0;// false

Reason:

1/0=Infinite;
1/-0=-Infinite;

Question:

Why isn't 1/0 or 1/-0 Undefined or NaN?

It can't be Infinity or -Infinity, because of 0 is equal to -0, so 1/0 is equal to 1/-0 I should say, but why it isn't? And why it isn't Undefined (what my calculator says) or NaN.

Sybsuper
  • 53
  • 6
  • 7
    Because IEEE-754 says so. – Sebastian Simon Nov 03 '18 at 09:11
  • Possible duplicate of [Why is Infinity-Infinity NaN?](https://stackoverflow.com/questions/32807110/why-is-infinity-infinity-nan) – Isma Nov 03 '18 at 09:11
  • 3
    Because the spec says so. Don't read any *mathematical* meaning into it, it's just the convention that was decided was more useful (to *programmers*) than alternatives – CertainPerformance Nov 03 '18 at 09:11
  • NaN = Not a number but in that operation you are working with numbers so it wouldn't make sense to divide two numbers and get a "Not a number" result. – Isma Nov 03 '18 at 09:12
  • 2
    Related: [Differentiating +0 and -0](https://stackoverflow.com/q/7223717/4642212) and [Are +0 and -0 the same?](https://stackoverflow.com/q/7223359/4642212). – Sebastian Simon Nov 03 '18 at 09:13

1 Answers1

5

This is because the IEEE 754 specifications define it like that.

There is however a reasoning for this: the affinely extended real number system extends the real numbers with the two infinities, which gives some more room for calculating with limits. So with this extension a division by zero is not undefined or NaN.

Consider that the following is true for positive x:

      limx→0(x) = limx→0(-x)

However the following is not true for positive x:

      limx→0(1/x) = limx→0(1/-x)

Note how the above comparisons with limit notation map to the comparisons you listed:

0 === -0;// true
1/0 === 1/-0;// false

Secondly, a division always maintains the following invariance: the result is negative if and only when exactly one of the operands is negative.

Both of these considerations give some credence as to why in IEEE 754:

1/0 === Infinity
1/-0 === -Infinity
trincot
  • 317,000
  • 35
  • 244
  • 286