user3117575
user3117575

Reputation:

Infinity - Infinity = NaN?

Any number minus itself should be 0, correct?

3 - 3 === 0

Then why

Infinity - Infinity === NaN

Because typeof Infinity is 'number':

Upvotes: 9

Views: 3542

Answers (6)

Jarred Apollo Niego
Jarred Apollo Niego

Reputation: 1

Infinity-Infinity is unknown because Infinity is endless, but it is Infinity-Infinity so, it might be 0 or Infinity

Upvotes: 0

Sazzad Hissain Khan
Sazzad Hissain Khan

Reputation: 40156

Infinity is not a Number. Its an idea, its a concept. Spend around 8 min to understand what is infinity from one of my favorite YouTube channels (Numberphile): https://www.youtube.com/watch?v=elvOZm0d4H0

Upvotes: 3

thefourtheye
thefourtheye

Reputation: 239453

As we know that, difference between two numbers can be calculated like this

a - b = a + (-b)

JavaScript uses this to find the difference between two values. Quoting from Applying the Additive Operators to Numbers section from ECMA 5.1 Specification,

The - operator performs subtraction when applied to two operands of numeric type, producing the difference of its operands; the left operand is the minuend and the right operand is the subtrahend. Given numeric operands a and b, it is always the case that a–b produces the same result as a +(–b).

So, when you do

Infinity - Infinity

it is evaluated as

Infinity + (-Infinity)

In JavaScript, they both are different Objects. Quoting from The Number Type section of ECMA 5.1 Specification,

There are two other special values, called positive Infinity and negative Infinity. For brevity, these values are also referred to for expository purposes by the symbols +∞ and −∞, respectively. (Note that these two infinite Number values are produced by the program expressions +Infinity (or simply Infinity) and -Infinity.)

Again, quoting from Applying the Additive Operators to Numbers section from ECMA 5.1 Specification

  • If either operand is NaN, the result is NaN.
  • The sum of two infinities of opposite sign is NaN.
  • The sum of two infinities of the same sign is the infinity of that sign.
  • ...

That is why the result is NaN.

Upvotes: 16

Tomalak
Tomalak

Reputation: 338158

The special number value Infinity encapsulates a concept.

It's meant for comparisons. By definition you can't do any arithmetic with it.

Assume a password expiry value. If you check the box "never expire", you could set the internal value to Infinity. Any comparison actualDate < expiryDate would evaluate to true (except of course when actualDate is Infinity itself).

That's a lot better than defining the "no expiryDate" state as an arbitrary value like 0 or -1 or null or undefined, where you have to maintain and remember what that value conceptually means in your application, introducing a new potential bug in every line where a date comparison happens.

Upvotes: 4

user2357112
user2357112

Reputation: 280426

For any number x, we should have x + 1 - x == 1, right? Well,

Infinity + 1 == Infinity

So what should Infinity + 1 - Infinity be? Is it 1? Then we have Infinity - Infinity == 1, which seems weird and arbitrary.

There is no infinity in the real numbers. There is an infinity in floating-point because it's convenient for some numerical algorithms to get a result when you do things like 1 / 0, but floating-point infinity cannot have all the nice properties you would like a number to have. In particular, there's no sensible number to return for Infinity - Infinity, so we get NaN.

Upvotes: 8

Jfach
Jfach

Reputation: 311

Correct. Infinity is not a number.

Upvotes: 1

Related Questions