Details of NaN
The constant NaN (not a number) can come up in a lot of situations. In AS3 it’s the default value of a Number field, it’s the result of division by zero in AS2, AS3, and JavaScript, and you can get it in a number of other ways. This article is about the reality of dealing with NaN.
Adobe neatly summarizes the ways you can encounter a NaN in AS3 in their language documentation. It applies to AS2 and JavaScript too. They don’t mention much about the results you’ll get if you attempt to use NaN though. With null, it’s mostly obvious: you get a crash. With NaN, there is no crash to indicate that something’s wrong. Adobe tells us:
The NaN value is not considered equal to any other value, including NaN, which makes it impossible to use the equality operator to test whether an expression is NaN. To determine whether a number is the NaN function, use isNaN().
And they are correct. It’s a bit surprising that NaN != NaN and NaN !== NaN since null == null, null === null, undefined == undefined, undefined === undefined, and null == undefined. Why no equality for NaN? I’m not sure. This leads us to write a convenience function ourselves:
function compareNumbers(n1:Number, n2:Number): Boolean { var n1isNaN:Boolean = isNaN(n1); var n2isNaN:Boolean = isNaN(n2); if (n1isNaN) { // Both would have to be NaN return n2isNaN; } else if (n2isNaN) { // Already established n1 is not NaN return false; } // Both are not NaN, just use the equality operator return n1 == n2; }
The above is both safe and slow. Use it with caution. Here it is in action:
trace(compareNumbers(NaN, NaN)); // true trace(compareNumbers(NaN, 3)); // false trace(compareNumbers(3, NaN)); // false trace(compareNumbers(3, 3)); // true trace(compareNumbers(3, 4)); // false
Now, how about operators other than equality and inequality? Try to guess what these lines would print:
trace(NaN > 0); trace(NaN >= 0); trace(NaN < 0); trace(NaN <= 0); trace(NaN > NaN); trace(NaN >= NaN); trace(NaN < NaN); trace(NaN <= NaN);
Answer: they all print false! Comparing NaN with anything results in false. It’s a little bizarre that NaN is neither less than, greater than, or equal to zero or any other constant including NaN, but that’s the way it is. OK, now try to guess the result of some arithmetic operators:
trace(NaN + 2); trace(NaN - 2); trace(NaN * 2); trace(NaN / 2); trace(NaN % 2); trace(NaN + NaN); trace(NaN - NaN); trace(NaN * NaN); trace(NaN / NaN); trace(NaN % NaN);
Answer: NaN in every case. At least it’s easy to remember. You simply can’t do arithmetic with a non-number. You always get NaN as a result.
There isn’t much more to NaN. It seems to have been kept simple for a reason. All comparisons with NaN result in false and all arithmetic with NaN results in NaN. Keep this in mind when dealing with Numbers in AS2, AS3, or JavaScript. It’s not the same as null!
#1 by Robert Penner on September 16th, 2009 ·
1/0 is Infinity. 0/0 is NaN. Also Math.sqrt(-1).
#2 by jackson on September 16th, 2009 ·
Strange. Thanks for pointing this out.