Reputation: 73
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.
Upvotes: 6
Views: 1607
Reputation: 350147
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
Upvotes: 6