• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <cmath>

    std::llround

    namespace std {
      long long llround(float x);       // (1) C++11からC++20まで
      long long llround(double x);      // (2) C++11からC++20まで
      long long llround(long double x); // (3) C++11からC++20まで
    
      constexpr long long
        llround(floating-point-type x); // (4) C++23
    
      long long
        llround(Integral x);            // (5) C++11
      constexpr long long
        llround(Integral x);            // (5) C++23
    
      long long
        llroundf(float x);              // (6) C++17
      constexpr long long
        llroundf(float x);              // (6) C++23
    
      long long
        llroundl(long double x);        // (7) C++17
      constexpr long long
        llroundl(long double x);        // (7) C++23
    }
    

    概要

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

    戻り値

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

    備考

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

    #include <iostream>
    #include <cmath>
    
    void test(double x)
    {
      long long result = std::llround(x);
      std::cout << "llround(" << 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);
    }
    

    出力

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

    バージョン

    言語

    • C++11

    処理系

    参照