fpclassify
來自 cppreference.com
定義於標頭檔案 <math.h> |
||
#define fpclassify(arg) /* implementation defined */ |
(C99 起) | |
將浮點值 arg 分類為以下類別:零、次正規、正規、無窮大、NAN 或實現定義的類別。該宏返回一個整數值。
FLT_EVAL_METHOD 被忽略:即使引數以比其型別更寬的範圍和更高的精度進行評估,它也會首先轉換為其語義型別,並且分類基於此:一個正規的 long double 值在轉換為 double 時可能會變為次正規,在轉換為 float 時可能會變為零。
目錄 |
[編輯] 引數
arg | - | 浮點值 |
[編輯] 返回值
返回 FP_INFINITE、FP_NAN、FP_NORMAL、FP_SUBNORMAL、FP_ZERO 或實現定義的型別之一,指定 arg 的類別。
[編輯] 示例
執行此程式碼
#include <float.h> #include <math.h> #include <stdio.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
[編輯] 參考
- C23 標準 (ISO/IEC 9899:2024)
- 7.12.3.1 fpclassify 宏 (p: TBD)
- C17 標準 (ISO/IEC 9899:2018)
- 7.12.3.1 fpclassify 宏 (p: TBD)
- C11 標準 (ISO/IEC 9899:2011)
- 7.12.3.1 fpclassify 宏 (p: 235)
- C99 標準 (ISO/IEC 9899:1999)
- 7.12.3.1 fpclassify 宏 (p: 216)
[編輯] 參閱
(C99) |
檢查給定數字是否具有有限值 (函式宏) |
(C99) |
檢查給定數字是否為無窮大 (函式宏) |
(C99) |
檢查給定數字是否為 NaN (函式宏) |
(C99) |
檢查給定數字是否為正常數 (函式宏) |