round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl
來自 cppreference.com
定義於標頭檔案 <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
|