Infinity - Infinity = NaN?

Any minus number should be 0 , right?

 3 - 3 === 0 

Then why

 Infinity - Infinity === NaN 

Because typeof Infinity is 'number' :

+6
source share
5 answers

As we know, the difference between two numbers can be calculated as follows:

 a - b = a + (-b) 

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

Operator - performs subtraction when applied to two operands of a numeric type, creating the difference of its operands; the left operand is a thumbnail, and the right operand is a subtraction. Given the numeric operands a and b, it always happens that ab produces the same result as + (- b).

So when you do

 Infinity - Infinity 

estimated as

 Infinity + (-Infinity) 

In JavaScript, they are both different objects. Quoted from the ECMA 5.1 Specification Number Type Section ,

There are two other special meanings: positive infinity and negative infinity . For brevity, these values ​​are also indicated for explanatory purposes by the symbols +∞ and βˆ’βˆž respectively. (Note that these two infinite Number values ​​are produced by programmatic expressions +Infinity (or just Infinity ) and -Infinity .)

Again, citing Application of the Additive Operators section to numbers from the ECMA 5.1 specification

  • If any of the operands is NaN , the result is NaN .
  • The sum of two infinities of the opposite sign NaN .
  • The sum of two infinities of the same sign is the infinity of this sign.
  • ...

That is why the result is NaN .

+13
source

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

 Infinity + 1 == Infinity 

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

In real numbers there is no infinity. There is infinity in the floating point, because for some numerical algorithms it is convenient to get the result when you do things like 1 / 0 , but with an infinite infinite floating point, there cannot be all the nice features that you would like to have. In particular, there is no reasonable number for Infinity - Infinity , so we get NaN .

+7
source

Infinity is not a number. His idea, her concept. Spend about 8 minutes to understand what infinity is from one of my favorite YouTube channels (Numberphile): https://www.youtube.com/watch?v=elvOZm0d4H0

+4
source

The value of the special Infinity number encapsulates the concept.

This meant for comparison. By definition, you cannot do arithmetic with it.

Assume a password expiration value. If you select the "Never expire" checkbox, you can set the internal value of Infinity . Any comparison of actualDate < expiryDate will be evaluated as true (except, of course, if actualDate is Infinity ).

This is much better than defining the β€œno expiryDate ” state as an arbitrary value, for example 0 or -1 or null or undefined , where you must maintain and remember what this value conceptually means in your application, presenting a new potential error on each line, where the date is compared.

+2
source

Right. Infinity is not a number.

+1
source

Source: https://habr.com/ru/post/978536/


All Articles