Check if number is NaN in javascript patch

Lango's picture

G'Day all

I was just wondering if anyone knows a clean way to check if a number is NaN.

I have tried 4 ways and only one returns true, but its a bit messy.

1/ (This way doesn't work)

var testNumber = NaN;
 
if (testNumber == undefined) // This returns false
   Log("testNumber is NaN"); 
else
   Log("testNumber was not detected as NaN)";

2/ (This way errors)

var testNumber = NaN;
 
if (isnan(testNumber)) // This errors as there is no isnan() method
   Log("testNumber is NaN"); 
else
   Log("testNumber was not detected as NaN)";

3/ (This way doesn't work)

var testNumber = NaN;
 
if (testNumber == NaN) // returns false
   Log("testNumber is NaN"); 
else
   Log("testNumber was not detected as NaN)";

4/ (This way works, but is not clean and makes me sad inside)

var testNumber = NaN;
var testNumber2 = testNumber || -9999;
 
if (testNumber == -9999) // returns true, as wanted.
   Log("testNumber is NaN"); 
else
   Log("testNumber was not detected as NaN)";

Anyone have any ideas?

Cheers

Lango

cwright's picture
Re: Check if number is NaN in javascript patch

As per IEEE-754 spec, a nan checked for equality against itself will be unequal (in other words, nan != nan)

in code, you can exploit this with the following:

var result = new Object();
function (__boolean isNan) main (__number aNumber)
{
   if(aNumber == aNumber)
      result.isNan = false;
   else
      result.isNan = true;
   return result;
}

(similar techniques work in C)

Lango's picture
Re: Check if number is NaN in javascript patch

Nice, thanks for that.

White Rose's picture
Re: Check if number is NaN in javascript patch

You have an error in the second way because you're using an incorrect function. The correct funcion name must be 'isNaN' (N is upper-case)

dimitri's picture
NaN ---- inf

When a division by zero occurs, is there any particular reason why the Math patch outputs < inf > as result, but the Mathematical expression < nan > ?

It calls for 2 different tests!

PreviewAttachmentSize
isNan_isFinite.qtz17.66 KB

cwright's picture
Re: NaN ---- inf

Math outputs inf because that's the correct result (division by zero results in +inf or -inf, depending on signs, according to the IEEE-754 spec). Expression doesn't because it probably does some internal processing on the output that massages infs into nans (QC doesn't handle nans very well, nor does it handle negative indexes very well -- this is a limitation of the Port classes, from my limited experimentation).

You're slightly limiting the isFinite function:

isFinite will return false for NaN and +/-inf (because neither of those are finite values). it will return true for all other floating point numbers (+/-0, subnormals, etc).

So if you're not really interested in nans specifically, just is isFinite, as it will fail nan and inf.

Sometimes inf is handy because you can still use it in mathematical expressions (most of the time this will kill your results, but there are cases where it's useful).

toon's picture
Re: Check if number is NaN in javascript patch

Like that ;-)

var test = NaN;

function checkNaN(n) { return !(n >= 0 || n < 0); }

checkNaN(test) => true

checkNaN(12) => false