C++ c++;测试double是否为#INF

C++ c++;测试double是否为#INF,c++,double,nan,C++,Double,Nan,我想隔离一些double值是NaN或#INF时的行为。要检测NaN,我需要测试 doubleVal != doubleVal 一个#INF怎么样?当doubleVal是#INF时,此测试是否正确?如果您不使用c++11,则需要而不是,并更改相应的命名空间 #include <cmath> // or <boost/math/special_functions/fpclassify.hpp> // ... if(isinf(num)){ // .

我想隔离一些
double
值是
NaN
#INF
时的行为。要检测
NaN
,我需要测试

doubleVal != doubleVal

一个
#INF
怎么样?当
doubleVal
#INF
时,此测试是否正确?

如果您不使用c++11,则需要
而不是
,并更改相应的命名空间

#include <cmath> // or <boost/math/special_functions/fpclassify.hpp>
// ...

    if(isinf(num)){
        // ...
    }
#包括//或
// ...
if(isinf(num)){
// ...
}

如果您没有使用c++11,那么您将需要
而不是
,并更改相应的命名空间

#include <cmath> // or <boost/math/special_functions/fpclassify.hpp>
// ...

    if(isinf(num)){
        // ...
    }
#包括//或
// ...
if(isinf(num)){
// ...
}
Boost中还提供了一个处理浮点数据类型的简洁工具

#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);
模板bool是有限的(tz);
模板bool-isinf(T);
模板bool-isnan(T);
模板布尔值正常(T);
Boost中还提供了一个处理浮点数据类型的简洁工具

#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);
模板bool是有限的(tz);
模板bool-isinf(T);
模板bool-isnan(T);
模板布尔值正常(T);

1进行乘法怎么样。
一旦你确定它不是
NaN

您还可以使用和,但这些可能会产生一些开销,具体取决于它们的实现

第三种选择是使用C最大值宏(或等效的
std::numeric_limits
):

bool是inf\u或nan(双x)
{
返回!(x=-DBL_MAX);
}    

1进行乘法怎么样。
一旦你确定它不是
NaN

您还可以使用和,但这些可能会产生一些开销,具体取决于它们的实现

第三种选择是使用C最大值宏(或等效的
std::numeric_limits
):

bool是inf\u或nan(双x)
{
返回!(x=-DBL_MAX);
}    

此外,您可能应该使用
std::isnan
检查NaN,如果只是因为它更明确。此外,您可能应该使用
std::isnan
检查NaN,如果只是因为它更明确。我喜欢此替代解决方案我喜欢此替代解决方案