?
This document uses PHP Chinese website manual Release
在頭文件<math.h>中定義 | ||
---|---|---|
float roundf( float arg ); | (1) | (since C99) |
double round( double arg ); | (2) | (since C99) |
long double roundl( long double arg ); | (3) | (since C99) |
Defined in header <tgmath.h> | ||
#define round( arg ) | (4) | (since C99) |
Defined in header <math.h> | ||
long lroundf( float arg ); | (5) | (since C99) |
long lround( double arg ); | (6) | (since C99) |
long lroundl( long double arg ); | (7) | (since C99) |
Defined in header <tgmath.h> | ||
#define lround( arg ) | (8) | (since C99) |
Defined in header <math.h> | ||
long long llroundf( float arg ); | (9) | (since C99) |
long long llround( double arg ); | (10) | (since C99) |
long long llroundl( long double arg ); | (11) | (since C99) |
Defined in header <tgmath.h> | ||
#define llround( arg ) | (12) | (since C99) |
1-3)計算最接近的整數(shù)值arg
(以浮點(diǎn)格式),無論當(dāng)前的舍入模式如何,將距離零的中途情況四舍五入。
5-7,9-11)arg
無論當(dāng)前的舍入模式如何,計算最接近的整數(shù)值(以整數(shù)格式),從零中途舍入一半。
4,8,12)類型泛型宏:如果arg
有一個類型long double
,roundl
,lroundl
,llroundl
被調(diào)用。否則,如果arg
有整數(shù)類型或類型double
,round
,lround
,llround
被調(diào)用。否則roundf
,lroundf
,llroundf
叫,分別。
ARG | - | 浮點(diǎn)值 |
---|
如果沒有發(fā)生錯誤arg
,則返回距離零中途的四舍五入的最近整數(shù)值。
返回值
論據(jù)
如果發(fā)生域錯誤,則返回實現(xiàn)定義的值。
按照math_errhandling中的指定報告錯誤。
如果返回類型表示的范圍lround
或結(jié)果llround
超出范圍,則可能會出現(xiàn)域錯誤或范圍錯誤。
如果實現(xiàn)支持IEEE浮點(diǎn)運(yùn)算(IEC 60559),對于round
,roundf
和roundl
功能:
當(dāng)前的舍入模式不起作用。
如果arg
是±∞,則返回,未修改
如果arg
為±0,則返回,未修改
如果arg
是NaN,則返回NaN
對于lround
和llround
功能家庭:
FE_INEXACT
從未被提出
當(dāng)前的舍入模式不起作用。
如果arg
是±∞,FE_INVALID
則引發(fā)并返回實現(xiàn)定義的值
如果舍入的結(jié)果超出返回類型的范圍,FE_INVALID
則會引發(fā)并返回實現(xiàn)定義的值
如果arg
是NaN,FE_INVALID
則引發(fā)并返回實現(xiàn)定義的值
FE_INEXACT
可能是(但不要求)round
在舍入非整數(shù)有限值時引發(fā)。
最大的可表示浮點(diǎn)值是所有標(biāo)準(zhǔn)浮點(diǎn)格式中的精確整數(shù),因此round
不會自行溢出; 但是intmax_t
,當(dāng)存儲在整數(shù)變量中時,結(jié)果可能會溢出任何整數(shù)類型(包括)。
POSIX規(guī)定是,所有病例lround
或llround
提高FE_INEXACT
是域錯誤。
該行為的double
版本round
如同執(zhí)行如下:
#include <math.h>#include <fenv.h>#pragma STDC FENV_ACCESS ON double round(double x){ fenv_t save_env; feholdexcept(&save_env); double result = rint(x); if (fetestexcept(FE_INEXACT)) { fesetround(FE_TOWARDZERO); result = rint(copysign(0.5 + fabs(x), x)); } feupdateenv(&save_env); return result;}
#include <stdio.h>#include <math.h>#include <fenv.h>#include <limits.h> #pragma STDC FENV_ACCESS ON 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)); // 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.0round(-2.3) = -2.0 round(-2.5) = -3.0 round(-2.7) = -3.0round(-0.0) = -0.0round(-Inf) = -inflround(+2.3) = 2 lround(+2.5) = 3 lround(+2.7) = 3lround(-2.3) = -2 lround(-2.5) = -3 lround(-2.7) = -3lround(-0.0) = 0lround(-Inf) = -9223372036854775808lround(LONG_MAX+1.5) = -9223372036854775808 FE_INVALID was raised
C11標(biāo)準(zhǔn)(ISO / IEC 9899:2011):
7.12.9.6輪次函數(shù)(p:253)
7.12.9.7地下和地下功能(p:253)
7.25類型通用數(shù)學(xué)<tgmath.h>(p:373-375)
F.10.6.6循環(huán)函數(shù)(p:527)
F.10.6.7基礎(chǔ)和基本功能(p:528)
C99標(biāo)準(zhǔn)(ISO / IEC 9899:1999):
7.12.9.6輪次函數(shù)(p:233)
7.12.9.7理論和實踐(p:234)
7.22類型通用數(shù)學(xué)<tgmath.h>(p:335-337)
F.9.6.6循環(huán)函數(shù)(p:464)
F.9.6.7地下和地下功能(p:464)