std::round, std::roundf, std::roundl, std::lround, std::lroundf, std::lroundl, std::llround, std::llroundf
来自cppreference.com
<tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
<tbody class="t-dcl-rev t-dcl-rev-num ">
</tbody><tbody>
</tbody>
| 在标头 <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 的最接近整数(以浮点数格式),中点情况舍入为远离零,无关乎当前舍入模式。标准库提供所有以无 cv 限定的浮点数类型作为实参 num 的类型的 std::round 重载。(C++23 起)4-9) 计算
num 的最接近整数(以整数格式),中点情况舍入为远离零,无关乎当前舍入模式。标准库提供所有以无 cv 限定的浮点数类型作为实参 num 的类型的 std::lround 和 std::llround 重载。(C++23 起)A-C) 为所有整数类型提供额外重载,将它们当做
double。参数
| num | - | 浮点数或整数 |
返回值
如果没有发生错误,那么返回 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::llround 引发 FE_INEXACT 的所有情况都是定义域错误。
std::round 的 double 版本表现为如同实现如下:
#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';
// 错误处理
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\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
参阅
(C++11)(C++11) |
不大于给定值的最接近整数 (函数) |
(C++11)(C++11) |
不小于给定值的最接近整数 (函数) |
(C++11)(C++11)(C++11) |
绝对值不大于给定值的最接近整数 (函数) |
round 的 C 文档
| |