跳到主要内容

Fpclassify

定义于头文件 <cmath> 中。

描述

将浮点值 num 分类为以下类别:

  • ,
  • 非正常数,
  • 正常数,
  • 无穷大,
  • 非数字 (NAN),
  • 实现定义的类别

该库为所有 cv-unqualified 浮点类型提供了 std::fpclassify 的重载,作为参数 num 的类型 (自 C++23 起).

为所有整数类型提供了额外的重载,它们被视为 double

声明

// 1)
constexpr int fpclassify( /* floating-point-type */ num );
附加重载
// 2)
template< class Integer >
constexpr int fpclassify( Integer num );

参数

num - 浮点或整数值

返回值

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

备注

不需要完全按照额外的重载提供额外的重载。
它们只需要足够,以确保对于其整数类型的参数 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

Fpclassify

定义于头文件 <cmath> 中。

描述

将浮点值 num 分类为以下类别:

  • ,
  • 非正常数,
  • 正常数,
  • 无穷大,
  • 非数字 (NAN),
  • 实现定义的类别

该库为所有 cv-unqualified 浮点类型提供了 std::fpclassify 的重载,作为参数 num 的类型 (自 C++23 起).

为所有整数类型提供了额外的重载,它们被视为 double

声明

// 1)
constexpr int fpclassify( /* floating-point-type */ num );
附加重载
// 2)
template< class Integer >
constexpr int fpclassify( Integer num );

参数

num - 浮点或整数值

返回值

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

备注

不需要完全按照额外的重载提供额外的重载。
它们只需要足够,以确保对于其整数类型的参数 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