如何在 C++ 中检查 float 或 double 是否为 NaN

从 C++11 开始,检查 NaN 很简单:

isnan_example.cpp
#include <numeric>

bool isNaN = std::isnan(myNumber);

请参见 std::isnan 文档作为参考。

你也可以使用 <cmath> 中的 C99 函数 isnan

isnan_c_example.cpp
#include <cmath>

bool isNaN = isnan(myDouble);

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