【发布时间】:2012-04-24 14:58:34
【问题描述】:
当某些double 值为NaN 或#INF 时,我想隔离该行为。为了检测NaN,我测试了
doubleVal != doubleVal
#INF 呢?当doubleVal 是#INF 时,这个测试是否成立?
【问题讨论】:
当某些double 值为NaN 或#INF 时,我想隔离该行为。为了检测NaN,我测试了
doubleVal != doubleVal
#INF 呢?当doubleVal 是#INF 时,这个测试是否成立?
【问题讨论】:
如果您不使用 c++11,那么您将需要 <boost/math/special_functions/fpclassify.hpp> 而不是 <cmath>,并进行相应的命名空间更改。
#include <cmath> // or <boost/math/special_functions/fpclassify.hpp>
// ...
if(isinf(num)){
// ...
}
【讨论】:
std::isnan 来检查 NaN,如果只是因为它更明确的话。
Boost 中还有一个header-only library,它拥有处理浮点数据类型的简洁工具
#include <boost/math/special_functions/fpclassify.hpp>
您将获得以下功能:
template <class T> bool isfinite(T z);
template <class T> bool isinf(T t);
template <class T> bool isnan(T t);
template <class T> bool isnormal(T t);
【讨论】: