float Nan

how to treat a flaot as Nan?
method1:
float x;
if(x!=x){"x is a Nan"}

method2:
float x;
if((uint32*)(x)&0x7FFFFFFF > 0x7F000000){"x is a Nan"}


Why the two method are right?
Who is better?
std::isnan is better:

http://www.cplusplus.com/reference/cmath/isnan/
http://en.cppreference.com/w/cpp/numeric/math/isnan

that will use the most appropriate way to perform that test on your target platform, such as the ucomiss CPU instruction

if(x!=x){"x is a Nan"} is okay too and might even compile to the same code.

(uint32*)(x)&0x7FFFFFFF > 0x7F000000 has undefined behavior.
Last edited on
Topic archived. No new replies allowed.