C++ 参考手册

位置:首页 > C++ 参考手册 >数值库 >常用数学函数 > std::fpclassify

定义于头文件 <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 - 浮点值

返回值

指明 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

参阅

(C++11)
检查给定数是否拥有有限值
(函数)
(C++11)
检查给定数是否为无限
(函数)
(C++11)
检查给定的数是否 NaN
(函数)
(C++11)
检查给定数是否正规
(函数)
提供查询所有基础数值类型的性质的接口。
(类模板)