Question

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?

Was it helpful?

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

OTHER TIPS

if (value !== value) {
    // Definitely NaN
}

You can make it as a function like this

function isNaN(inputValue) {
    return inputValue !== inputValue;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top