round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl
来自 cppreference.cn
定义于头文件 <math.h> |
||
float roundf( float arg ); |
(1) | (C99 起) |
double round( double arg ); |
(2) | (C99 起) |
long double roundl( long double arg ); |
(3) | (C99 起) |
定义于头文件 <tgmath.h> |
||
#define round( arg ) |
(4) | (C99 起) |
定义于头文件 <math.h> |
||
long lroundf( float arg ); |
(5) | (C99 起) |
long lround( double arg ); |
(6) | (C99 起) |
long lroundl( long double arg ); |
(7) | (C99 起) |
定义于头文件 <tgmath.h> |
||
#define lround( arg ) |
(8) | (C99 起) |
定义于头文件 <math.h> |
||
long long llroundf( float arg ); |
(9) | (C99 起) |
long long llround( double arg ); |
(10) | (C99 起) |
long long llroundl( long double arg ); |
(11) | (C99 起) |
定义于头文件 <tgmath.h> |
||
#define llround( arg ) |
(12) | (C99 起) |
1-3) 计算最接近 arg 的整数值(以浮点格式),将中间值四舍五入到远离零的方向,而不考虑当前的舍入模式。
5-7, 9-11) 计算最接近 arg 的整数值(以整数格式),将中间值四舍五入到远离零的方向,而不考虑当前的舍入模式。
4,8,12) 类型通用宏:如果 arg 的类型是 long double,则调用
roundl
、lroundl
、llroundl
。否则,如果 arg 的类型是整数类型或 double,则调用 round
、lround
、llround
。否则,分别调用 roundf
、lroundf
、llroundf
。目录 |
[编辑] 参数
arg | - | 浮点值 |
[编辑] 返回值
如果未发生错误,则返回最接近 arg 的整数值,将中间值四舍五入到远离零的方向。
返回值
参数
若发生定义域错误,返回实现定义的值。
[编辑] 错误处理
错误按 math_errhandling
中指定的方式报告。
如果 lround
或 llround
的结果超出了返回类型可表示的范围,则可能发生定义域错误或范围错误。
如果实现支持 IEEE 浮点算术 (IEC 60559)
- 对于
round
、roundf
和roundl
函数- 当前的舍入模式无效。
- 如果 arg 是 ±∞,则返回它,不作修改。
- 如果 arg 是 ±0,则返回它,不作修改。
- 如果 arg 是 NaN,则返回 NaN。
- 对于
lround
和llround
系列函数- FE_INEXACT 从不引发。
- 当前的舍入模式无效。
- 如果 arg 是 ±∞,则会引发 FE_INVALID 并返回实现定义的值。
- 如果舍入结果超出了返回类型的范围,则会引发 FE_INVALID 并返回实现定义的值。
- 如果 arg 是 NaN,则会引发 FE_INVALID 并返回实现定义的值。
[编辑] 注意
当对非整数有限值进行舍入时,round
可能会(但不是必须)引发 FE_INEXACT。
所有标准浮点格式中最大的可表示浮点值都是精确整数,因此 round
本身不会溢出;但是,当存储到整数变量中时,结果可能会溢出任何整数类型(包括 intmax_t)。
POSIX 指定,所有 lround
或 llround
引发 FE_INVALID 的情况都是定义域错误。
round
的 double 版本行为如同以下实现:
[编辑] 示例
运行此代码
#include <assert.h> #include <fenv.h> #include <float.h> #include <limits.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON double custom_round(double x) { return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5); } void test_custom_round() { const double sample[] = { 0.0, 2.3, 2.5 - DBL_EPSILON, 2.5, 2.5 + DBL_EPSILON, 2.7, INFINITY }; for (size_t t = 0; t < sizeof sample / sizeof(double); ++t) assert(round(+sample[t]) == custom_round(+sample[t]) && round(-sample[t]) == custom_round(-sample[t])); } int main(void) { // round printf("round(+2.3) = %+.1f ", round(2.3)); printf("round(+2.5) = %+.1f ", round(2.5)); printf("round(+2.7) = %+.1f\n", round(2.7)); printf("round(-2.3) = %+.1f ", round(-2.3)); printf("round(-2.5) = %+.1f ", round(-2.5)); printf("round(-2.7) = %+.1f\n", round(-2.7)); printf("round(-0.0) = %+.1f\n", round(-0.0)); printf("round(-Inf) = %+f\n", round(-INFINITY)); test_custom_round(); // lround printf("lround(+2.3) = %+ld ", lround(2.3)); printf("lround(+2.5) = %+ld ", lround(2.5)); printf("lround(+2.7) = %+ld\n", lround(2.7)); printf("lround(-2.3) = %+ld ", lround(-2.3)); printf("lround(-2.5) = %+ld ", lround(-2.5)); printf("lround(-2.7) = %+ld\n", lround(-2.7)); printf("lround(-0.0) = %+ld\n", lround(-0.0)); printf("lround(-Inf) = %+ld\n", lround(-INFINITY)); // FE_INVALID raised // error handling feclearexcept(FE_ALL_EXCEPT); printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX + 1.5)); if (fetestexcept(FE_INVALID)) puts(" FE_INVALID was raised"); }
可能的输出
round(+2.3) = +2.0 round(+2.5) = +3.0 round(+2.7) = +3.0 round(-2.3) = -2.0 round(-2.5) = -3.0 round(-2.7) = -3.0 round(-0.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 lround(LONG_MAX+1.5) = -9223372036854775808 FE_INVALID was raised
[编辑] 参考
- C23 标准 (ISO/IEC 9899:2024)
- 7.12.9.6 round 函数 (p: TBD)
- 7.12.9.7 lround 和 llround 函数 (p: TBD)
- 7.25 类型通用数学 <tgmath.h> (p: TBD)
- F.10.6.6 round 函数 (p: TBD)
- F.10.6.7 lround 和 llround 函数 (p: TBD)
- C17 标准 (ISO/IEC 9899:2018)
- 7.12.9.6 round 函数 (p: 184)
- 7.12.9.7 lround 和 llround 函数 (p: 184-185)
- 7.25 类型通用数学 <tgmath.h> (p: 272-273)
- F.10.6.6 round 函数 (p: 384)
- F.10.6.7 lround 和 llround 函数 (p: 385)
- C11 标准 (ISO/IEC 9899:2011)
- 7.12.9.6 round 函数 (p: 253)
- 7.12.9.7 lround 和 llround 函数 (p: 253)
- 7.25 类型通用数学 <tgmath.h> (p: 373-375)
- F.10.6.6 round 函数 (p: 527)
- F.10.6.7 lround 和 llround 函数 (p: 528)
- C99 标准 (ISO/IEC 9899:1999)
- 7.12.9.6 round 函数 (p: 233)
- 7.12.9.7 lround 和 llround 函数 (p: 234)
- 7.22 类型通用数学 <tgmath.h> (p: 335-337)
- F.9.6.6 round 函数 (p: 464)
- F.9.6.7 lround 和 llround 函数 (p: 464)
[编辑] 参阅
(C99)(C99) |
计算不大于给定值的最大整数 (function) |
(C99)(C99) |
计算不小于给定值的最小整数 (function) |
(C99)(C99)(C99) |
将数字截断到最接近的整数,其绝对值不大于给定值 (function) |
C++ 文档 关于 round
|