std::fpclassify
來自 cppreference.com
定義於標頭檔案 <cmath> |
||
(1) | ||
int fpclassify( float num ); int fpclassify( double num ); |
(C++11 起) (直至 C++23) |
|
constexpr int fpclassify( /* floating-point-type */ num ); |
(C++23 起) | |
定義於標頭檔案 <cmath> |
||
template< class Integer > int fpclassify( Integer num ); |
(A) | (C++11 起) (constexpr since C++23) |
1) 將浮點值 num 分類為以下類別:零、非正常、正常、無窮大、NAN 或實現定義的類別。 庫為所有 cv 非限定浮點型別提供了
std::fpclassify
的過載作為引數 num 的型別。(C++23 起)A) 為所有整數型別提供了額外的過載,它們被視為 double。
目錄 |
[編輯] 引數
num | - | 浮點值或整數值 |
[編輯] 返回值
返回 FP_INFINITE、FP_NAN、FP_NORMAL、FP_SUBNORMAL、FP_ZERO 之一,或實現定義的型別,指定 num 的類別。
[編輯] 注意
不需要完全按照 (A) 提供額外的過載。它們只需足以確保對於其整型引數 num,std::fpclassify(num) 具有與 std::fpclassify(static_cast<double>(num)) 相同的效果。
[編輯] 示例
執行此程式碼
#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++11) |
檢查給定數字是否為無窮大 (函式) |
(C++11) |
檢查給定數字是否為 NaN (函式) |
(C++11) |
檢查給定數字是否為正常數 (函式) |
提供查詢所有基本數值型別屬性的介面 (類模板) | |
C documentation for fpclassify
|