std::fpclassify

来自cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
 
在标头 <cmath> 定义
(1)
int fpclassify(float num );

int fpclassify(double num );

int fpclassify(longdouble num );
(C++11 起)
(C++23 前)
constexprint fpclassify(/* floating-point-type */ num );
(C++23 起)
在标头 <cmath> 定义
template<class Integer >
int fpclassify( Integer num );
(A) (C++11 起)
(C++23 起为 constexpr)
1) 归类浮点数 num 到下列类别中:零、非正规、正规、无穷大、NaN 或实现定义类别。标准库提供所有以无 cv 限定的浮点数类型作为参数 num 的类型的 std::fpclassify 重载。(C++23 起)
A) 为所有整数类型提供额外重载,将它们当做 double

目录

[编辑]参数

num - 浮点数或整数

[编辑]返回值

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

[编辑]注解

额外重载不需要以 (A) 的形式提供。它们只需要能够对它们的整数类型实参 num 确保 std::fpclassify(num)std::fpclassify(static_cast<double>(num)) 的效果相同。

[编辑]示例

#include <cfloat>#include <cmath>#include <iostream>   auto show_classification(double x){switch(std::fpclassify(x)){caseFP_INFINITE:return"无穷大";caseFP_NAN:return"非数";caseFP_NORMAL:return"正规值";caseFP_SUBNORMAL:return"非正规值";caseFP_ZERO:return"零";default:return"未知值";}}   int main(){std::cout<<"1.0/0.0 是"<< show_classification(1/0.0)<<'\n'<<"0.0/0.0 是"<< show_classification(0.0/0.0)<<'\n'<<"DBL_MIN/2 是"<< show_classification(DBL_MIN/2)<<'\n'<<"-0.0 是"<< show_classification(-0.0)<<'\n'<<"1.0 是"<< show_classification(1.0)<<'\n';}

输出:

1.0/0.0 是无穷大 0.0/0.0 是非数 DBL_MIN/2 是非正规值 -0.0 是零 1.0 是正规值

[编辑]参阅

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