std::fpclassify
From 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以降) (C++23 以降 constexpr) |
1) 浮動小数点数
numを、ゼロ、非正規数、正規数、無限大、NaN(非数)、または実装定義のカテゴリに分類します。ライブラリは、パラメータnumの型として、すべてのcv修飾されていない浮動小数点型に対するstd::fpclassifyのオーバーロードを提供します。(C++23以降)A) すべての整数型に対する追加のオーバーロードが提供されます。これらは double として扱われます。
目次 |
[edit] パラメータ
| num | - | 浮動小数点数または整数値 |
[edit] 戻り値
numのカテゴリを指定する、FP_INFINITE、FP_NAN、FP_NORMAL、FP_SUBNORMAL、FP_ZEROのいずれか、または実装定義の型。
[edit] 注意
追加のオーバーロードは、(A)とまったく同じように提供される必要はありません。それらは、整数型の引数numに対して、std::fpclassify(num)がstd::fpclassify(static_cast<double>(num))と同じ効果を持つことを保証するために十分なものであればよいのです。
[edit] 例
このコードを実行
#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
[edit] 関連項目
| (C++11) |
与えられた数値が有限値かチェックする (関数) |
| (C++11) |
与えられた数値が無限大かチェックする (関数) |
| (C++11) |
与えられた数値が NaN かチェックする (関数) |
| (C++11) |
与えられた数値が正規化数かチェックする (関数) |
| 全ての基本数値型のプロパティを問い合わせるインターフェースを提供する (クラステンプレート) | |
| C言語のドキュメント (fpclassify)
| |