名前空間
変種
操作

std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf

From cppreference.com
< cpp‎ | numeric‎ | math
 
 
 
共通の数学関数
関数
基本的な数学関数
(C++11)  
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
指数関数
(C++11)
(C++11)

(C++11)
(C++11)
べき乗関数
(C++11)
(C++11)
三角関数と
双曲線関数
(C++11)
(C++11)
(C++11)

誤差関数とガンマ関数
(C++11)
(C++11)
(C++11)
(C++11)
最近接整数への浮動小数点数操作
roundlroundllround
(C++11)(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)(C++11)
浮動小数点数の操作関数
(C++11)(C++11)
(C++11)
(C++11)
(C++11)(C++11)
(C++11)
分類と比較
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
マクロ定数
分類
(C++11)(C++11)(C++11)(C++11)(C++11)


 
ヘッダー <cmath> で定義
浮動小数点型への丸め
(1)
float       round ( float num );

double      round ( double num );

long double round ( long double num );
(C++11以降)
(C++23まで)
constexpr /* floating-point-type */
            round ( /* floating-point-type */ num );
(C++23から)
float       roundf( float num );
(2) (C++11以降)
(C++23 以降 constexpr)
long double roundl( long double num );
(3) (C++11以降)
(C++23 以降 constexpr)
long への丸め
(4)
long lround ( float num );

long lround ( double num );

long lround ( long double num );
(C++11以降)
(C++23まで)
constexpr long lround( /* floating-point-type */ num );
(C++23から)
long lroundf( float num );
(5) (C++11以降)
(C++23 以降 constexpr)
long lroundl( long double num );
(6) (C++11以降)
(C++23 以降 constexpr)
long long への丸め
(7)
long long llround ( float num );

long long llround ( double num );

long long llround ( long double num );
(C++11以降)
(C++23まで)
constexpr long long llround( /* floating-point-type */ num );
(C++23から)
long long llroundf( float num );
(8) (C++11以降)
(C++23 以降 constexpr)
long long llroundl( long double num );
(9) (C++11以降)
(C++23 以降 constexpr)
ヘッダー <cmath> で定義
template< class Integer >
double round( Integer num );
(A) (C++11以降)
(C++23 以降 constexpr)
template< class Integer >
long lround( Integer num );
(B) (C++11以降)
(C++23 以降 constexpr)
template< class Integer >
long long llround( Integer num );
(C) (C++11以降)
(C++23 以降 constexpr)
1-3) num(浮動小数点数形式)に最も近い整数値を、現在の丸めモードに関係なく、半数ケース(x.5)はゼロから離れる方向に丸めて計算します。ライブラリは、パラメータ num の型として、すべての cv 修飾されていない浮動小数点型に対する std::round のオーバーロードを提供します。(since C++23)
4-9) num(整数形式)に最も近い整数値を、現在の丸めモードに関係なく、半数ケース(x.5)はゼロから離れる方向に丸めて計算します。ライブラリは、パラメータ num の型として、すべての cv 修飾されていない浮動小数点型に対する std::lround および std::llround のオーバーロードを提供します。(since C++23)
A-C) すべての整数型に対する追加のオーバーロードが提供されており、これらは double として扱われます。

目次

[編集] パラメータ

num - 浮動小数点数または整数値

[編集] 戻り値

エラーが発生しない場合、num に最も近い整数値が、半数ケース(x.5)はゼロから離れる方向に丸められて返されます。

戻り値
math-round away zero.svg
num

ドメインエラーが発生した場合、実装定義の値が返されます。

[編集] エラー処理

エラーは math_errhandling で指定された通りに報告される。

std::lround または std::llround の結果が、戻り値の型で表現できる範囲外の場合、ドメインエラーまたは範囲エラーが発生する可能性があります。

実装がIEEE浮動小数点算術 (IEC 60559) をサポートしている場合、

std::round 関数の場合
  • 現在の 丸めモード は影響しません。
  • num が ±∞ の場合、変更されずに返されます。
  • num が ±0 の場合、変更されずに返されます。
  • num が NaN の場合、NaN が返されます。
std::lround および std::llround 関数の場合
  • FE_INEXACT は決して設定されません。
  • 現在の 丸めモード は影響しません。
  • num が ±∞ の場合、FE_INVALID が設定され、実装定義の値が返されます。
  • 丸めの結果が戻り値の型の範囲外の場合、FE_INVALID が設定され、実装定義の値が返されます。
  • num が NaN の場合、FE_INVALID が設定され、実装定義の値が返されます。

[編集] 注釈

std::round は、非整数有限値を丸める際に FE_INEXACT を設定する場合があります(必須ではありません)。

表現可能な最大の浮動小数点値は、すべての標準浮動小数点形式において正確な整数であるため、std::round は単独ではオーバーフローしません。ただし、整数変数に格納する際に、結果が整数型(std::intmax_t を含む)をオーバーフローする可能性があります。

POSIX ではstd::lround または std::llroundFE_INEXACT を設定するすべてのケースはドメインエラーであると規定されています。

std::rounddouble バージョンは、以下のように実装されたかのように動作します。

#include <cfenv>
#include <cmath>
 
#pragma STDC FENV_ACCESS ON
 
double round(double x)
{
    const int save_round = std::fegetround();
    std::fesetround(FE_TOWARDZERO);
    const double result = std::rint(std::copysign(0.5 + std::fabs(x), x));
    std::fesetround(save_round);
    return result;
}

追加のオーバーロードは、(A-C) とまったく同じように提供される必要はありません。引数 num が整数型である場合に、以下の条件を確実に満たすだけで十分です。

  • std::round(num) は、std::round(static_cast<double>(num)) と同じ効果を持ちます。
  • std::lround(num) は、std::lround(static_cast<double>(num)) と同じ効果を持ちます。
  • std::llround(num) は、std::llround(static_cast<double>(num)) と同じ効果を持ちます。

[編集]

#include <cassert>
#include <cfenv>
#include <cfloat>
#include <climits>
#include <cmath>
#include <iostream>
 
// #pragma STDC FENV_ACCESS ON
 
double custom_round(double x)
{
    const int save_round = std::fegetround();
    std::fesetround(FE_TOWARDZERO);
    const double result = std::rint(std::copysign(0.5 + std::fabs(x), x));
    std::fesetround(save_round);
    return result;
}
 
void test_custom_round()
{
    for (const double x :
        {
            0.0, 0.3,
            0.5 - DBL_EPSILON / 2,
            0.5,
            0.5 + DBL_EPSILON / 2,
            0.7, 1.0, 2.3, 2.5, 2.7, 3.0,
            static_cast<double>(INFINITY)
        })
        assert(round(+x) == custom_round(+x) && round(-x) == custom_round(-x));
}
 
int main()
{
    test_custom_round();
 
    std::cout << std::showpos;
 
    // round
    std::cout << "round(+2.3) = " << std::round(2.3)
              << "  round(+2.5) = " << std::round(2.5)
              << "  round(+2.7) = " << std::round(2.7) << '\n'
              << "round(-2.3) = " << std::round(-2.3)
              << "  round(-2.5) = " << std::round(-2.5)
              << "  round(-2.7) = " << std::round(-2.7) << '\n';
 
    std::cout << "round(-0.0) = " << std::round(-0.0)  << '\n'
              << "round(-Inf) = " << std::round(-INFINITY) << '\n';
 
    // lround
    std::cout << "lround(+2.3) = " << std::lround(2.3)
              << "  lround(+2.5) = " << std::lround(2.5)
              << "  lround(+2.7) = " << std::lround(2.7) << '\n'
              << "lround(-2.3) = " << std::lround(-2.3)
              << "  lround(-2.5) = " << std::lround(-2.5)
              << "  lround(-2.7) = " << std::lround(-2.7) << '\n';
 
    std::cout << "lround(-0.0) = " << std::lround(-0.0)  << '\n'
              << "lround(-Inf) = " << std::lround(-INFINITY) << '\n';
 
    // error handling
    std::feclearexcept(FE_ALL_EXCEPT);
 
    std::cout << "std::lround(LONG_MAX+1.5) = "
              << std::lround(LONG_MAX + 1.5) << '\n';
    if (std::fetestexcept(FE_INVALID))
        std::cout << "    FE_INVALID was raised\n";
}

実行結果の例

round(+2.3) = +2  round(+2.5) = +3  round(+2.7) = +3
round(-2.3) = -2  round(-2.5) = -3  round(-2.7) = -3
round(-0.0) = -0
round(-Inf) = -inf
lround(+2.3) = +2  lround(+2.5) = +3  lround(+2.7) = +3
lround(-2.3) = -2  lround(-2.5) = -3  lround(-2.7) = -3
lround(-0.0) = +0
lround(-Inf) = -9223372036854775808
std::lround(LONG_MAX+1.5) = -9223372036854775808
    FE_INVALID was raised

[編集] 関連項目

(C++11)(C++11)
与えられた値以下の最大の整数
(関数) [編集]
(C++11)(C++11)
与えられた値以上の最小の整数
(関数) [編集]
(C++11)(C++11)(C++11)
与えられた値の絶対値以下の最大の整数
(関数) [編集]
English 日本語 中文(简体) 中文(繁體)