How to check if float or double is NaN in C++

Checking for NaN is simple since C++11:

isnan_example.cpp
#include <numeric>

bool isNaN = std::isnan(myNumber);

See the std::isnan docs for reference.

You can also use the C99 function isnan from <cmath>:

isnan_c_example.cpp
#include <cmath>

bool isNaN = isnan(myDouble);

 


Check out similar posts by category: C/C++