Javascript's NaN
coderwall.com
coderwall.com
Don't mess with IEEE 754, it is almost universally adopted for binary floating-point for very good reasons.
The correct though unintuitive way to check for NaN is `(x === x) === false`. NaN is a number per the IEEE 754.
The majority of Javascript WTFs are due to type coercion. Use triple equals and check your inputs to avoid.
var x = "a";
isNaN(x); // true
(x === x) === false // false
x = NaN
isNaN(x); // true
(x === x) === false // true
Also, isNaN is global.
You should be harsher on him. NaN is definitely a number in practically every programming language.
(type-of (/ 0.0 0.0))
SINGLE-FLOAT
System.err.println((new Double(0.0/0.0)) instanceof Number)
true
from types import *
type(1e10000 / 1e10000)
<type 'float'>
It looks like the blogger is just out and out mistaken. But there's more. The poster goes on about NaN not being easy to check for: not realizing that it's the only value which is never equal to itself. He even thinks this is a bug.In this context, 'NaN' just happens to be another admissible value within the space of "number".
isNaN is the defined method of checking if something Not A Number or can't be converted into a number.
The Real WTF (and infamous "feature" of JS) is that many things can be converted into a number.
isNaN("string") => true
isNaN({}) => true
but you might not expect that isNaN("3e2") => false
isNaN("") => false
isNaN([]) => false Number.isNaN(NaN) => true
Number.isNaN(3e2) => false
Number.isNaN("NaN") => false
Number.isNaN("string") => false
Number.isNaN({}) => false
Number.isNaN("3e2") => false
Number.isNaN([]) => false
http://wiki.ecmascript.org/doku.php?id=harmony:number.isnan hackclj.core=> Double/NaN
NaN
hackclj.core=> (== Double/NaN Double/NaN)
false
hackclj.core=> (= Double/NaN Double/NaN)
false
hackclj.core=> (.equals Double/NaN Double/NaN)
true
hackclj.core=> (identical? Double/NaN Double/NaN)
false
hackclj.core=> (type Double/NaN)
java.lang.Double
hackclj.core=> (number? Double/NaN)
true
In Perl (which doesn't have a concept of a number type distinct from other scalars such as strings, but we can see that it participates in addition differently from non-numeric scalars, which behave like 0): DB<1> $inf = 1e300 * 1e300
DB<2> $nan = $inf - $inf
DB<3> print $nan
nan
DB<4> print ($nan == $nan)
DB<5> print ref($nan)
DB<6> print $nan+1
nan
In Ruby: irb(main):001:0> inf = 1e300*1e300
=> Infinity
irb(main):002:0> nan = inf-inf
=> NaN
irb(main):003:0> nan == nan
=> false
irb(main):004:0> nan.class
=> Float
Python 2.7.3 (much earlier versions got equality wrong, claiming nan == nan): >>> inf = 1e300*1e300
>>> nan = inf-inf
>>> nan
nan
>>> nan == nan
False
>>> type(nan)
<type 'float'>Try it in some other language you have lying around, like Python:
>>> type(float('nan'))
<type 'float'>
>>> float('nan') == float('nan')
False
Also Haskell: Prelude> 0/0
NaN
Prelude> 0/0 == 0/0
False
If anything, it's refreshing that JavaScript does behave like other languages in this case!Though if they're not calling them floats, maybe they could have also come up with a better name for NaN. If they used, say, "ERR", it would be something many people had seen before (from calculators) and we'd get to stop hearing people bitch about "NotANumber is a number WTF".
(I suppose "they" in this case must be the hypothetical well-treated team that didn't develop JavaScript instead of a frustrated Brendan Eich.)