名稱空間
變體
操作

round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl

來自 cppreference.com
< c‎ | 數值‎ | 數學
 
 
 
常用數學函式
函式
基本操作
(C99)
(C99)
(C99)
(C99)(C99)(C99)(C23)
最大值/最小值操作
(C99)
(C99)
指數函式
(C23)
(C99)
(C99)
(C23)
(C23)

(C99)
(C99)(C23)
(C23)
(C23)
冪函式
(C99)
(C23)
(C23)

(C99)
(C23)
(C23)
三角函式和雙曲函式
(C23)
(C23)
(C23)
(C23)
(C99)
(C99)
(C99)
最近整數浮點數
roundlroundllround
(C99)(C99)(C99)
(C99)

(C99)(C99)(C99)
(C23)(C23)(C23)(C23)
浮點數操作
(C99)(C99)
(C99)(C23)
(C99)
窄化操作
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
量子與量子指數
十進位制重新編碼函式
總序和載荷函式
分類
(C99)
(C99)
(C99)
(C23)
誤差函式和伽馬函式
(C99)
(C99)
(C99)
(C99)
型別
宏常量
特殊浮點值
(C99)(C23)
引數和返回值
錯誤處理
快速操作指示符
 
定義於標頭檔案 <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,則呼叫 roundllroundlllroundl。否則,如果 arg 的型別是整數型別或 double,則呼叫 roundlroundllround。否則,分別呼叫 roundflroundfllroundf

目錄

[編輯] 引數

arg - 浮點值

[編輯] 返回值

如果未發生錯誤,則返回最接近 arg 的整數值,將中間值四捨五入到遠離零的方向。

返回值
math-round away zero.svg
引數

若發生定義域錯誤,返回實現定義的值。

[編輯] 錯誤處理

錯誤按 math_errhandling 中指定的方式報告。

如果 lroundllround 的結果超出了返回型別可表示的範圍,則可能發生定義域錯誤或範圍錯誤。

如果實現支援 IEEE 浮點算術 (IEC 60559)

對於 roundroundfroundl 函式
  • 當前的舍入模式無效。
  • 如果 arg 是 ±∞,則返回它,不作修改。
  • 如果 arg 是 ±0,則返回它,不作修改。
  • 如果 arg 是 NaN,則返回 NaN。
對於 lroundllround 系列函式
  • FE_INEXACT 從不引發。
  • 當前的舍入模式無效。
  • 如果 arg 是 ±∞,則會引發 FE_INVALID 並返回實現定義的值。
  • 如果舍入結果超出了返回型別的範圍,則會引發 FE_INVALID 並返回實現定義的值。
  • 如果 arg 是 NaN,則會引發 FE_INVALID 並返回實現定義的值。

[編輯] 注意

當對非整數有限值進行舍入時,round 可能會(但不是必須)引發 FE_INEXACT

所有標準浮點格式中最大的可表示浮點值都是精確整數,因此 round 本身不會溢位;但是,當儲存到整數變數中時,結果可能會溢位任何整數型別(包括 intmax_t)。

POSIX 指定,所有 lroundllround 引發 FE_INVALID 的情況都是定義域錯誤。

rounddouble 版本行為如同以下實現:

#include <math.h>
#pragma STDC FENV_ACCESS ON
 
double round(double x)
{
    return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5);
}

[編輯] 示例

#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)

[編輯] 參閱

計算不大於給定值的最大整數
(function) [編輯]
(C99)(C99)
計算不小於給定值的最小整數
(function) [編輯]
(C99)(C99)(C99)
將數字截斷到最接近的整數,其絕對值不大於給定值
(function) [編輯]
C++ 文件 關於 round