Namespaces
Variants

FP_NORMAL, FP_SUBNORMAL, FP_ZERO, FP_INFINITE, FP_NAN

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
FP_NORMAL FP_SUBNORMAL FP_ZERO FP_INFINITE FP_NAN
(C99) (C99) (C99) (C99) (C99)
Error handling
Fast operation indicators
定義済みヘッダー <math.h>
#define FP_NORMAL    /*implementation defined*/
(C99以降)
#define FP_SUBNORMAL /*implementation defined*/
(C99以降)
#define FP_ZERO      /*implementation defined*/
(C99以降)
#define FP_INFINITE  /*implementation defined*/
(C99以降)
#define FP_NAN       /*implementation defined*/
(C99以降)

FP_NORMAL FP_SUBNORMAL FP_ZERO FP_INFINITE FP_NAN マクロは、それぞれ浮動小数点数における異なる分類を表します。これらはすべて整数定数式に展開されます。

定数 説明
FP_NORMAL 値が 正規 であることを示す(無限大、非正規化数、非数、ゼロではない)
FP_SUBNORMAL 値が 非正規化数 であることを示す
FP_ZERO 値が正のゼロまたは負のゼロであることを示す
FP_INFINITE 値が基礎となる型で表現不可能であることを示す(正または負の無限大)
FP_NAN 値が非数(NaN)であることを示す

#include <stdio.h>
#include <math.h>
#include <float.h>
const char *show_classification(double x) {
    switch(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(void)
{
    printf("1.0/0.0 is %s\n", show_classification(1/0.0));
    printf("0.0/0.0 is %s\n", show_classification(0.0/0.0));
    printf("DBL_MIN/2 is %s\n", show_classification(DBL_MIN/2));
    printf("-0.0 is %s\n", show_classification(-0.0));
    printf(" 1.0 is %s\n", show_classification(1.0));
}

出力:

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

参考文献

  • C17規格 (ISO/IEC 9899:2018):
  • 7.12/6 FP_NORMAL, ... (p: 169-170)
  • C11規格 (ISO/IEC 9899:2011):
  • 7.12/6 FP_NORMAL, ... (p: 232)
  • C99規格 (ISO/IEC 9899:1999):
  • 7.12/6 FP_NORMAL, ... (p: 213)

関連項目

指定された浮動小数点値を分類する
(関数マクロ)
C++ documentation for FP_categories