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 或实现定义类别。参数
arg | - | 浮点值 |
返回值
指明 arg
类别的 FP_INFINITE 、 FP_NAN 、 FP_NORMAL 、 FP_SUBNORMAL 、 FP_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) |
检查给定数是否正规 (函数) |
提供查询所有基础数值类型的性质的接口。 (类模板) |