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

履歴 編集

function
<cmath>

std::nexttoward(C++11)

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

  constexpr floating-point-type
    nexttoward(floating-point-type x,
               floating-point-type y); // (4) C++23

  Promoted
    nexttoward(Integral x,
               long double y);         // (5) C++11
  constexpr Promoted
    nexttoward(Integral x,
               floating-point-type y); // (5) C++23

  float
    nexttowardf(float x,
                long double y);        // (6) C++17
  constexpr float
    nexttowardf(float x,
                long double y);        // (6) C++23

  long double
    nexttowardl(long double x,
                long double y);        // (7) C++17
  constexpr long double
    nexttowardl(long double x,
                long double y);        // (7) C++23
}

概要

指定方向への次の表現可能な値を取得する。

この関数は、パラメータxの値をパラメータyの値の方向に対して、その環境で表現可能な最小の値だけ進める。

この関数はパラメータyの型がlong double固定であることを除いて、std::nextafter()関数と等価である (C++23以降では精度規定の関数を除いてylong doubleではなく任意の浮動小数点数型であるため、std::nextafter()関数と等価である)。

  • (1) : floatに対するオーバーロード
  • (2) : doubleに対するオーバーロード
  • (3) : long doubleに対するオーバーロード
  • (4) : 浮動小数点数型に対するオーバーロード
  • (5) : 算術型に対するオーバーロード (大きい精度にキャストして計算される。整数はdoubleで計算される)
  • (6) : float型規定
  • (7) : long double型規定

戻り値

パラメータxの値をパラメータxの方向に、表現可能な最小の値だけ進めた値を返す。

xyが等値である場合、yを返す。

進めた結果が無限大、もしくは表現できない場合、値域エラーとなる。

備考

  • C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された

#include <iostream>
#include <cmath>

int main()
{
  float result1 = std::nexttoward(0.0f, 1.0L);
  std::cout << result1 << std::endl;

  float result2 = std::nexttoward(0.0f, -1.0L);
  std::cout << result2 << std::endl;
}

出力例

1.4013e-45
-1.4013e-45

備考

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

  • GCC 4.6.1 以上

バージョン

言語

  • C++11

処理系

参照