Testing if Float value is NaN

kp11 picture kp11 · Jan 20, 2011 · Viewed 33.9k times · Source

Possible Duplicate:
Checking if a double (or float) is nan in C++

I have a requirement to check if float is NaN. By going through some of the links I found the most common check.

FLOAT32 f32_test_NaN = (some_value);
if (f32_test_NaN == f32_test_NaN)
{
    //do something;
}
else
{
    // do something;
}

But this does not seem to work for me. My code is as follows:

FLOAT32 test_NaN = 0x414570A3;//some value - is this ok?

Debugging on GDB:

(gdb) p test_NaN
$1 = 1.09506982e+09

(gdb) p/x test_NaN
$2 = 0x41457080 // Hex is not same as init value - What is compiler doing?

So in my case test_NaN is equal to test_NaN.

Please let me know if any compiler setting has to be done. I am running on solaris. Or is there any other way to check the same.

Thanks in advance.

Answer

SiegeX picture SiegeX · Jan 20, 2011

Include math.h and use int isnan(x). Don't forget to link with -lm