宏/函式
<cmath> <ctgmath>

fpclassify

fpclassify(x)
函式
int fpclassify (float x);int fpclassify (double x);int fpclassify (long double x);
對浮點值進行分類
返回一個 int 型別的值,該值與其中一個分類宏常量匹配,具體取決於 x 的值

描述
FP_INFINITE正無窮大或負無窮大 (溢位)
FP_NAN非數字 (Not-A-Number)
FP_ZERO零值
FP_SUBNORMAL次正規值 (下溢)
FP_NORMAL正規值 (非以上任何情況)
請注意,每個值僅屬於一個類別:零不是正規值。

這些 int 型別的宏常量在標頭檔案 <cmath> (<math.h>) 中定義。

在 C 語言中, ഇത്宏實現,但 x 的型別應為 floatdoublelong double
在 C++ 中,它是為每個浮點型別透過函式過載實現的。

引數

x
要分類的值。

返回值

以下 int 值之一:FP_INFINITEFP_NANFP_ZEROFP_SUBNORMALFP_NORMAL

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/* fpclassify example */
#include <stdio.h>      /* printf */
#include <math.h>       /* fpclassify, signbit, FP_* */

int main()
{
  double d = 1.0 / 0.0;
  switch (fpclassify(d)) {
    case FP_INFINITE:  printf ("infinite");  break;
    case FP_NAN:       printf ("NaN");       break;
    case FP_ZERO:      printf ("zero");      break;
    case FP_SUBNORMAL: printf ("subnormal"); break;
    case FP_NORMAL:    printf ("normal");    break;
  }
  if (signbit(d)) printf (" negative\n");
  else printf (" positive or unsigned\n");
  return 0;
}

輸出

infinite positive or unsigned


另見