cplusplus
.com
TUTORIALS
REFERENCE
ARTICLES
FORUM
C++
Tutorials
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs
Forum
General C++ Programming
float Nan
float Nan
Aug 8, 2016 at 1:04pm UTC
Wu zhen hai
(112)
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?
Aug 8, 2016 at 2:06pm UTC
Cubbi
(4774)
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
Aug 8, 2016 at 2:13pm UTC
Topic archived. No new replies allowed.