[webkit-dev] Check for NaN value

Darin Adler darin at apple.com
Mon Mar 16 13:59:39 PDT 2009


On Mar 16, 2009, at 1:48 PM, Husam Senussi wrote:

> I'm trying to find out API or method to check for NaN value in  
> JavaScriptCore.

If you are writing code to add to JavaScriptCore and need a function  
to tell you if a value is NaN, you can call the standard C library  
isnan function from <math.h>. To work around problems with  
environments that don’t have this function, the file using isnan can  
include the <wtf/MathExtras.h> instead of <math.h>. The WTF  
MathExtras.h has the responsibility of ensuring it’s defined even if  
the system <math.h> doesn’t have it.

It’s even better to write code that doesn’t have to specifically check  
for NaN when possible. There are all sorts of ways to do this based on  
the behavior of NaN defined by the IEEE standard. For example, if you  
have code that should run only if the number is > 0 and not NaN, you  
can write:

     if (x > 0)

And be guaranteed that it will be false if the number is a NaN. This  
is usually more efficient than an explicit isnan function call.

     -- Darin



More information about the webkit-dev mailing list