Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Testing if Float value is NaN [duplicate]

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.

like image 559
kp11 Avatar asked Jan 20 '11 07:01

kp11


2 Answers

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

like image 111
SiegeX Avatar answered Oct 20 '22 03:10

SiegeX


If <math.h> is not available, then do this:

if (x != x)
{
    // x is NaN
}
like image 45
Oliver Charlesworth Avatar answered Oct 20 '22 01:10

Oliver Charlesworth