- cmath[meta header]
- std[meta namespace]
- function[meta id-type]
- cpp11[meta cpp]
namespace std {
long long llround(float x);
long long llround(double x);
long long llround(long double x);
long long llround(Integral x);
long long int llroundf(float x); // C++17 から
long long int llroundl(long double x); // C++17 から
}- Integral[italic]
引数 x を四捨五入により丸めた整数値をlong long型として得る。
ここで引数 x の四捨五入とは、x を最も近い整数に丸めるが、x の小数部分が 0.5 の場合には、x の符号が正負のいずれであってもゼロから遠い方向に丸めることを指す。
具体例は下記の出力例を参照。
引数 x を四捨五入により丸めた整数値を、long long型型の範囲に収めて返す。
- 本関数は、C99 の規格にある
llround(より正確にはmath.hヘッダのllround、llroundf、llroundlの 3 つ。それぞれ C++ のdouble、float、long doubleバージョンに相当)と等価である。 round関数と違い、本関数において戻り値が非整数型引数xと異なる場合に、例外FE_INEXACTを発生させる必要はない。- 戻り値が
long long型型の範囲を超えた場合、定義域エラーが起こる可能性がある。その際の挙動については、<cmath>を参照。 - なお、本関数の挙動は、現在の丸めモードには依存しない。
#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);
}- std::llround[color ff0000]
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
- Clang, C++11 mode: 3.0
- GCC, C++11 mode: 4.3.6
- ICC: ??
- Visual C++: ??