Search code examples
javascriptmozillaieee-754spidermonkeyasm.js

Checking for NaNs in asm.js code


How can I efficiently check in asm.js code whether a floating point value is a NaN?

A way that works in principle is to import the global isNaN JavaScript function as a foreign function into the asm.js module. As calling foreign function is expensive, though, so this won't yield optimal code.

Comparing to the value NaN (which is part of the standard library) is no option because the comparision of a NaN to another NaN always yields false by JavaScript semantics.

Examining bits on the heap is also no option because the endianess is not specified.

Why is isNaN not part of the standard library in asm.js?


Solution

  • This property of NaN is commonly used to identify it :

    var isNaN = a!=a; 
    

    From Wikipedia :

    The equality and inequality predicates are non-signaling so x = x returning false can be used to test if x is a quiet NaN