Skip to content

Latest commit

 

History

History
87 lines (66 loc) · 2.68 KB

isnan.md

File metadata and controls

87 lines (66 loc) · 2.68 KB

isnan

  • cmath[meta header]
  • std[meta namespace]
  • function[meta id-type]
  • cpp11[meta cpp]
namespace std {
  bool isnan(float x);            // (1) C++11からC++20まで
  bool isnan(double x);           // (2) C++11からC++20まで
  bool isnan(long double x);      // (3) C++11からC++20まで

  constexpr bool
    isnan(floating-point-type x); // (4) C++23

  bool
    isnan(Integral x);            // (5) C++11
  constexpr bool
    isnan(Integral x);            // (5) C++23
}
  • Integral[italic]

概要

数値が NaN であるか判定する。

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

戻り値

パラメータxがNaNである場合、trueを返す。そうでない場合、falseを返す。

備考

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

#include <cassert>
#include <cmath>
#include <limits>

int main()
{
  bool result1 = std::isnan(std::numeric_limits<float>::quiet_NaN());
  bool result2 = std::isnan(std::numeric_limits<float>::signaling_NaN());

  assert(result1);
  assert(result2);
}
  • std::isnan[color ff0000]
  • quiet_NaN()[link /reference/limits/numeric_limits/quiet_nan.md]
  • signaling_NaN()[link /reference/limits/numeric_limits/signaling_nan.md]

出力

備考

特定の環境では、早期に constexpr 対応されている場合がある:

  • GCC 4.6.1 以上

バージョン

言語

  • C++11

処理系

参照