Namespaces
Variants

std:: fpclassify

From cppreference.net
Common mathematical functions
Nearest integer floating point operations
(C++11)
(C++11)
(C++11) (C++11) (C++11)
Floating point manipulation functions
(C++11) (C++11)
(C++11)
(C++11)
Classification and comparison
fpclassify
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
定义于头文件 <cmath>
(1)
int fpclassify ( float num ) ;

int fpclassify ( double num ) ;

int fpclassify ( long double num ) ;
(C++11 起)
(C++23 前)
constexpr int fpclassify ( /* 浮点类型 */ num ) ;
(C++23 起)
定义于头文件 <cmath>
template < class Integer >
int fpclassify ( Integer num ) ;
(A) (C++11 起)
(C++23 起为 constexpr)
1) 将浮点值 num 分类为以下类别:零、次正规数、正规数、无穷大、NAN 或实现定义类别。 库为所有 cv 未限定浮点类型提供了 std::fpclassify 的重载,作为参数 num 的类型。 (since 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 文档 for fpclassify