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