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    /* implementation defined */
(C++11以降)
#define FP_SUBNORMAL /* implementation defined */
(C++11以降)
#define FP_ZERO      /* implementation defined */
(C++11以降)
#define FP_INFINITE  /* implementation defined */
(C++11以降)
#define FP_NAN       /* implementation defined */
(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ドキュメント for FP_categories