std::fpclassify_C++中文网

定义于头文件 <cmath>

int fpclassify( float arg );

(1) (C++11 起)

int fpclassify( double arg );

(2) (C++11 起)

int fpclassify( long double arg );

(3) (C++11 起)

int fpclassify( IntegralType arg );

(4) (C++11 起)

1-3) 归类浮点值 arg 到下列类别中:零、非正规、正规、无穷大、 NaN 或实现定义类别。

4) 接受任何整数类型 from 参数的重载集或函数模板。等价于 (2) (将参数转型为 double )。

参数

返回值

指明 arg 类别的 FP_INFINITEFP_NANFP_NORMALFP_SUBNORMALFP_ZERO 或实现定义类型之一。

示例

#include <iostream>
#include <cmath>
#include <cfloat>
 
const char* show_classification(double x) {
    switch(std::fpclassify(x)) {
        case FP_INFINITE:  return "Inf";
        case FP_NAN:       return "NaN";
        case FP_NORMAL:    return "normal";
        case FP_SUBNORMAL: return "subnormal";
        case FP_ZERO:      return "zero";
        default:           return "unknown";
    }
}
int main()
{
    std::cout << "1.0/0.0 is " << show_classification(1/0.0) << '\n'
              << "0.0/0.0 is " << show_classification(0.0/0.0) << '\n'
              << "DBL_MIN/2 is " << show_classification(DBL_MIN/2) << '\n'
              << "-0.0 is " << show_classification(-0.0) << '\n'
              << "1.0 is " << show_classification(1.0) << '\n';
}

输出:

1.0/0.0 is Inf
0.0/0.0 is NaN
DBL_MIN/2 is subnormal
-0.0 is zero
1.0 is normal

参阅