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 の符号が正負のいずれであってもゼロから遠い方向に丸めることを指す。
具体例は下記の出力例を参照。
- (1) :
floatに対するオーバーロード - (2) :
doubleに対するオーバーロード - (3) :
long doubleに対するオーバーロード - (4) : 浮動小数点数型に対するオーバーロード
- (5) : 整数型に対するオーバーロード (
doubleにキャストして計算される) - (6) :
float型規定 - (7) :
long double型規定
戻り値
引数 x を四捨五入により丸めた整数値を、long long型の範囲に収めて返す。
備考
- 本関数は、C99 の規格にある
llround(より正確にはmath.hヘッダのllround、llroundf、llroundlの 3 つ。それぞれ C++ のdouble、float、long 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
処理系
- Clang: 3.0 ✅
- GCC: 4.3.6 ✅
- ICC: ??
- Visual C++: ??
関連項目
参照
- P0533R9 constexpr for
<cmath>and<cstdlib>- C++23での、一部関数の
constexpr対応
- C++23での、一部関数の
- P1467R9 Extended floating-point types and standard names