1

As far as I remember I have been aware of this peculiarity where the type of NaN is a number. Now, some people go out of their way to defend this as a 'common computer science principle', but in the end it still feels wrong to me (just because something is defined under the section numeric types doesn't make it a number).

Either way, this made me wonder, are there any typical situations where this makes code easier to read? Normally it just means you have to add on an extra check that it's a number and not NaN which is quite a hassle. And for the life of me I can't remember ever having run into a situation where I wanted typeof NaN to return number. So, did I miss something? Or is it really as bad as it looks?

Community
  • 1
  • 1
David Mulder
  • 26,123
  • 9
  • 51
  • 114

1 Answers1

3

In all kinds of code. The constant NaN is explicitly of type "number". The value NaN should not be taken to mean what the colloquial phrase "not a number" means. Instead, it's a specific term from the IEEE floating point spec that describes a family of values whose bit arrangements are such that the values cannot be interpreted meaningfully by the rules of the spec. Therefore, they're numbers but they're "not a number" numbers.

It does make things somewhat weird, but it's in a way not that much different than what you run into dealing with strings and the empty string. It's just a value of a primitive type that you (probably) need to worry about. Generally, getting a NaN from some computation probably means that your computation needs a validity test somewhere upstream.

Now, one thing that is a little flaky is the fact that JavaScript uses NaN to report on non-number values that cannot be converted to numbers. It would probably have been more robust and semantically correct for such situations to throw exceptions, but that's water under the bridge at this point.

Pointy
  • 405,095
  • 59
  • 585
  • 614
  • So, in what kind of code do you in real life then run `typeof x == 'number'` without adding on `not isNaN(x)`? if there is any chance of `x` being `NaN` ? – David Mulder Apr 29 '15 at 22:46
  • @DavidMulder I just updated the answer; I don't deny that your complaint is valid :) – Pointy Apr 29 '15 at 22:47
  • I am curious then, in other languages would casting a string to an int/parsing a string as an int through a function return a NaN value, 0 or throw? As in, Javascript would, but no idea in other languages from the top of my head. – David Mulder Apr 29 '15 at 22:49
  • @DavidMulder well there are an awful lot of other languages :) Personally I am not a big fan of exceptions as a fundamental design thing, but if a language does have exceptions then number conversion sure seems like a good time for an exception. – Pointy Apr 29 '15 at 22:52
  • Yeah, was just wondering about the big families :P If you would have said "Yeah, most other languages do not misuse `NaN` for that" then that would have been quite an eye opener for me. Oh well, might go and try it myself next time I have my full dev environment up and running (instead of the cloud based IDE I work in lately). – David Mulder Apr 29 '15 at 22:54
  • @DavidMulder well for a particular example, Java throws exceptions at the drop of a hat. – Pointy Apr 29 '15 at 22:57