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 被引发,并返回实现定义的值。
[编辑] 注意
FE_INEXACT 可能(但不要求)在 round
舍入非整数有限值时引发。
最大的可表示浮点值在所有标准浮点格式中都是精确的整数,因此 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) |
计算不大于给定值的最大整数 (函数) |
(C99)(C99) |
计算不小于给定值的最小整数 (函数) |
(C99)(C99)(C99) |
舍入到最接近零的整数,其大小不大于给定值 (函数) |
C++ 文档 关于 round
|