How to determine if double/float is NaN?

Asked 2 years ago, Updated 2 years ago, 134 views

Does C++ have a function like ishnan()? C was in <math.h> but C++ does not have <cmath>?

*Oh, I'm using MinGW.

c++ double nan

2022-09-21 16:04

1 Answers

<cmath> in C++11 supports std::isan(). Returns true if the factor is NaN, or false if not.

bool isnan( float arg ); (since C++11)
bool isnan( double arg ); (since C++11)
bool isnan( long double arg ); (since C++11)

According to IEEE standard, NaN value is always false when comparing NaN value.

This means that f!=f is always true for float/doublef.

In principle, this method should work for a compiler that follows IEEE floating point I'm not sure that optimization is followed. It is recommended that you check and write in advance with the compiler.


2022-09-21 16:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.