[webkit-dev] Check for NaN value

Paul Pedriana ppedriana at gmail.com
Mon Mar 16 18:44:30 PDT 2009


More generally, all NaN comparisons return false, even with 
binary-identical NaNs. So if x is a NaN, then (x == x) is false, as long 
as the compiler doesn't try to eliminate the expression, and I don't 
think it's allowed to for floating point (I don't have a reference 
handy, though).

bool IsNaN(float x)
{
    return x != x;
}

Paul


> 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
>
> _______________________________________________
> webkit-dev mailing list
> webkit-dev at lists.webkit.org
> http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev
>



More information about the webkit-dev mailing list