Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 725
Marc Avatar asked Sep 19 '26 11:09

Marc


1 Answers

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

like image 119
Denys Séguret Avatar answered Sep 21 '26 01:09

Denys Séguret