0

Few experiments

I am going to write some Maths expressions in JS and wondered:

typeof(Infinity)              // "number", in Maths it is NOT a number
typeof(NaN)                   // "number", how NaN (Not A Number) can actually be a number?

And just a few more experiments:

Infinity === Infinity         // TRUE
Infinity * 2 === Infinity * 5 // TRUE, so 2===5 if Infinity !== 0?

However, this makes sense to me:

NaN * 2 === NaN * 5           // FALSE

Questions

  • Why JS is designed so? Was there a particular reason for that?
  • Are there potentials threats when writing Maths expressions in JS?
igorpavlov
  • 3,576
  • 6
  • 29
  • 56
  • They *are* floating point values - of type `number`. You will find it in every other language that uses standard floating point math. – Bergi Nov 15 '15 at 17:44
  • What you expect `Infinity * 2` and `Infinity * 5` to evaluate to such that you would not have `Infinity * 2 === Infinity * 5`? Think about this for a second. By definition, there is no number larger than `Infinity`. So, what should `Infinity * 2` be? This ones seems entirely logical. How `NaN` behaves, on the other hand, has rarely seemed logical to me. It is what it is an you may have to just learn how it works. No idea why they made it the way they did. – jfriend00 Nov 15 '15 at 17:44
  • See if this makes sense to you: `NaN === NaN // FALSE` – Ryan Wheale Nov 15 '15 at 17:48
  • 1
    Wait, `0 * 2 == 0 * 5`, and in contrast to Infinity, `0` really is a number for sure, so that's a proof that `2 == 5`? – Bergi Nov 15 '15 at 17:49
  • @igorpavlov: `Infinity === 1/0`, so you need to extend your care about `0` in reasoning to Infinity. – Bergi Nov 15 '15 at 17:53
  • Now that makes sense. – igorpavlov Nov 15 '15 at 17:54
  • @igorpavlov: Yes, `Infinity !== 0`. They're about half-way (in layman's terms, I am not a mathematician) as far away from each other as they possibly could be. (Only half way? Yes, because `0` is half-way [I am not a mathematician] between `-Infinity` and `Infinity`.) – T.J. Crowder Nov 15 '15 at 17:55
  • Well, Infinity - Infinity === NaN, so definitely not "half" way :D – igorpavlov Nov 15 '15 at 17:56
  • @igorpavlov: Conceptually, I mean, not in IEEE-754's calculation matrix. :-) (I have to say I'm a bit surprised, I would have figured `Infinity - Infinity` was `Infinity`, but IANAM.) – T.J. Crowder Nov 15 '15 at 17:57
  • Math.pow(-Infinity, Infinity) is Infinity. So that means Infinity is NEVER a odd number. Insane. Shouldn't it be NaN? – igorpavlov Nov 15 '15 at 18:32

2 Answers2

2

Why JS is designed so? Was there a particular reason for that?

JavaScript takes both NaN (including the fact that it's never equal to itself) and infinity directly from IEEE-754, the go-to standard for floating point numbers in computing, which is also used by many other languages. (Specifically, it uses a "quiet NaN".) IEEE-754, in turn, gets a lot of this from mathematic theory (e.g., Infinity + 1 === Infinity, apparently this is considered true for some kinds of numbers, but I am not a mathematician; I would assume the designers of the standard had a reason for following that definition).

Are there potentials threats when writing Maths expressions in JS?

I wouldn't call them threats, but: NaN will propagate throughout any calculation where it comes up. Other than that, you have the usual issues with precision discussed in this question and its answers.

Community
  • 1
  • 1
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I take offense to the ∞+1 remark. In most mathematical contexts,"infinity plus one" is not defined or meaningless. Among those where it may make sense, the statement false as often as it is true. One example where it's false are the transfinite ordinals (ω+1 > ω). So it is demonstrably false that ∞+1=∞ is "standard math". IEEE 754 infinities behave the way they do because that's the best option for common numeric algorithms, not because of any deep connection to a mathematical theory. –  Nov 15 '15 at 17:58
  • @delnan: In comments on the question I said "I am not a mathematician" several times. Clearly, I should have put that in the answer! :-) – T.J. Crowder Nov 15 '15 at 17:59
  • @delnan: And now I have, along with a nice weaselly Wikipedia link. :-) – T.J. Crowder Nov 15 '15 at 18:03
  • 1
    I couldn't have asked for more :) –  Nov 15 '15 at 18:04
1

There's an answer about inequality of two NaNs here - Why is NaN === NaN false?. As for the infinities, this is a pure math - infinity times anything is infinity. Lastly, typeof returns "number" for Infinity and NaN just because these are constants of numeric type in JS.

Community
  • 1
  • 1
ap3rus
  • 91
  • 3
  • This is *not* pure math. It may be a layperson's understanding of math, but both mathematicians and the engineers behind IEEE 754 know that "infinity" is a really vague and overloaded concept and the various ways it can be made precise are more complicated than what this answer describes. –  Nov 15 '15 at 18:03