最終更新日時(UTC):
が更新

履歴 編集

function
<cmath>

std::fpclassify(C++11)

namespace std {
  int fpclassify(float x);             // (1) C++11からC++20まで
  int fpclassify(double x);            // (2) C++11からC++20まで
  int fpclassify(long double x);       // (3) C++11からC++20まで

  constexpr int
    fpclassify(floating-point-type x); // (4) C++23

  int
    fpclassify(Integral x);            // (5) C++11
  constexpr int
    fpclassify(Integral x);            // (5) C++23
}

概要

指定された値の数値分類を返す。

  • (1) : floatに対するオーバーロード
  • (2) : doubleに対するオーバーロード
  • (3) : long doubleに対するオーバーロード
  • (4) : 浮動小数点数型に対するオーバーロード
  • (5) : 整数型に対するオーバーロード (doubleにキャストして計算される)

戻り値

引数 x で指定された値の数値分類を表すマクロの整数値を返す。

引数 x で指定された値の数値分類 戻り値
正、あるいは、負の無限大の場合 FP_INFINITE
NaN(Not a Number、非数)の場合 FP_NAN
正規化数の場合 FP_NORMAL
非正規化数の場合 FP_SUBNORMAL
正、あるいは、負のゼロの場合 FP_ZERO

なお、上記のいずれにも属さない処理系定義の数値分類がある場合、その数値分類に対応するマクロの整数値が返る。

備考

  • C標準ライブラリではfpclassifyは関数マクロとして定義されるが、C++標準ライブラリでは関数として定義される
  • C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された

#include <iostream>
#include <limits>
#include <cmath>

int main()
{
  std::cout << std::boolalpha;
  std::cout << (std::fpclassify(std::numeric_limits<double>::infinity()  ) == FP_INFINITE ) << std::endl;
  std::cout << (std::fpclassify(std::numeric_limits<double>::quiet_NaN() ) == FP_NAN      ) << std::endl;
  std::cout << (std::fpclassify(1.0                                      ) == FP_NORMAL   ) << std::endl;
  std::cout << (std::fpclassify(std::numeric_limits<double>::denorm_min()) == FP_SUBNORMAL) << std::endl;
  std::cout << (std::fpclassify(0.0                                      ) == FP_ZERO     ) << std::endl;
}

出力例

true
true
true
true
true

参照