std::fpclassify
来自 cppreference.cn
| 定义于头文件 <cmath> |
||
| (1) | ||
int fpclassify( float num ); int fpclassify( double num ); |
(C++11 起) (直至 C++23) |
|
| constexpr int fpclassify( /* floating-point-type */ num ); |
(C++23 起) | |
| 定义于头文件 <cmath> |
||
| template< class Integer > int fpclassify( Integer num ); |
(A) | (C++11 起) (constexpr since C++23) |
1) 将浮点值 num 分类为以下类别:零、非正常、正常、无穷大、NAN 或实现定义的类别。 库为所有 cv 非限定浮点类型提供了
std::fpclassify 的重载作为参数 num 的类型。(C++23 起)A) 为所有整数类型提供了额外的重载,它们被视为 double。
目录 |
[编辑] 参数
| num | - | 浮点值或整数值 |
[编辑] 返回值
返回 FP_INFINITE、FP_NAN、FP_NORMAL、FP_SUBNORMAL、FP_ZERO 之一,或实现定义的类型,指定 num 的类别。
[编辑] 注意
不需要完全按照 (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)) { 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) |
检查给定数字是否为正常数 (函数) |
| 提供查询所有基本数值类型属性的接口 (类模板) | |
| C documentation for fpclassify
| |