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

履歴 編集

function
<cmath>

std::lround(C++11)

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

  constexpr long
    lround(floating-point-type x); // (4) C++23

  long
    lround(Integral x);            // (5) C++11
  constexpr long
    lround(Integral x);            // (5) C++23

  long int
    lroundf(float x);              // (6) C++17
  constexpr long int
    lroundf(float x);              // (6) C++23

  long int
    lroundl(long double x);        // (7) C++17
  constexpr long int
    lroundl(long double x);        // (7) C++23
}

概要

引数 x を四捨五入により丸めた整数値をlong型として得る。
ここで引数 x の四捨五入とは、x を最も近い整数に丸めるが、x の小数部分が 0.5 の場合には、x の符号が正負のいずれであってもゼロから遠い方向に丸めることを指す。
具体例は下記の出力例を参照。

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

戻り値

引数 x を四捨五入により丸めた整数値を、long型の範囲に収めて返す。

備考

  • 本関数は、C99 の規格にある lround(より正確には math.h ヘッダの lroundlroundflroundl の 3 つ。それぞれ C++ の doublefloatlong double バージョンに相当)と等価である。
  • round関数と違い、本関数において戻り値が非整数型引数 x と異なる場合に、例外 FE_INEXACT を発生させる必要はない。
  • 戻り値がlong型の範囲を超えた場合、定義域エラーが起こる可能性がある。その際の挙動については、<cmath> を参照。
  • なお、本関数の挙動は、現在の丸めモードには依存しない。
  • C++23では、(1)、(2)、(3)が(4)に統合され、拡張浮動小数点数型を含む浮動小数点数型へのオーバーロードとして定義された

#include <iostream>
#include <cmath>

void test(double x)
{
  long result = std::lround(x);
  std::cout << "lround(" << x << ") = " << result << std::endl;
}

int main()
{
  test(2.0);
  test(2.1);
  test(2.5);
  test(2.9);
  test(-2.0);
  test(-2.1);
  test(-2.5);
  test(-2.9);
}

出力

lround(2) = 2
lround(2.1) = 2
lround(2.5) = 3
lround(2.9) = 3
lround(-2) = -2
lround(-2.1) = -2
lround(-2.5) = -3
lround(-2.9) = -3

バージョン

言語

  • C++11

処理系

参照