Namespaces
Variants

FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN

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
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Types
(C++11)
(C++11)
(C++11)
Macro constants
Classification
FP_NORMAL FP_SUBNORMAL FP_ZERO FP_INFINITE FP_NAN
(C++11) (C++11) (C++11) (C++11) (C++11)


定义于头文件 <cmath>
#define FP_NORMAL    /* 由实现定义 */
(C++11 起)
#define FP_SUBNORMAL /* 由实现定义 */
(C++11 起)
#define FP_ZERO      /* 由实现定义 */
(C++11 起)
#define FP_INFINITE  /* 由实现定义 */
(C++11 起)
#define FP_NAN       /* 由实现定义 */
(C++11 起)

FP_NORMAL FP_SUBNORMAL FP_ZERO FP_INFINITE FP_NAN 宏分别表示不同类别的浮点数。它们均展开为整型常量表达式。

常量 说明
FP_NORMAL 表示该值为 规约形式 ,即不是无穷大、非规约形式、非数字或零
FP_SUBNORMAL 表示该值为 非规约形式
FP_ZERO 表示该值为正零或负零
FP_INFINITE 表示该值无法用底层类型表示(正无穷或负无穷)
FP_NAN 表示该值为非数字(NaN)

示例

#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 文档 关于 FP_categories