©
本文档使用
php.cn手册 发布
在头文件<math.h>中定义 | ||
---|---|---|
float rintf( float arg ); | (1) | (since C99) |
double rint( double arg ); | (2) | (since C99) |
long double rintl( long double arg ); | (3) | (since C99) |
Defined in header <tgmath.h> | ||
#define rint( arg ) | (4) | (since C99) |
Defined in header <math.h> | ||
long lrintf( float arg ); | (5) | (since C99) |
long lrint( double arg ); | (6) | (since C99) |
long lrintl( long double arg ); | (7) | (since C99) |
Defined in header <tgmath.h> | ||
#define lrint( arg ) | (8) | (since C99) |
Defined in header <math.h> | ||
long long llrintf( float arg ); | (9) | (since C99) |
long long llrint( double arg ); | (10) | (since C99) |
long long llrintl( long double arg ); | (11) | (since C99) |
Defined in header <tgmath.h> | ||
#define llrint( arg ) | (12) | (since C99) |
1-3)arg
使用当前舍入模式将浮点参数四舍五入为浮点格式的整数值。
5-7,9-11)arg
使用当前舍入模式将浮点参数四舍五入为整数格式的整数值。
4,8,12)类型泛型宏:如果arg
有一个类型long double
,rintl
,lrintl
,llrintl
被调用。否则,如果arg
有整数类型或类型double
,rint
,lrint
,llrint
被调用。否则rintf
,lrintf
,llrintf
叫,分别。
arg | - | 浮点值 |
---|
如果没有发生错误,则返回arg
根据当前舍入模式最接近的整数值。
按照math_errhandling中的指定报告错误。
如果返回类型表示的范围lrint
或结果llrint
超出范围,则可能会出现域错误或范围错误。
如果实现支持IEEE浮点运算(IEC 60559),则对于rint
函数:
如果arg
是±∞,则返回,未修改
如果arg
为±0,则返回,未修改
如果arg
是NaN,则返回NaN
为lrint
和llrint
功能:
如果arg
是±∞,FE_INVALID
则引发并返回实现定义的值
如果舍入的结果超出返回类型的范围,FE_INVALID
则会引发并返回实现定义的值
如果arg
是NaN,FE_INVALID
则引发并返回实现定义的值
POSIX规定是,所有病例lrint
或llrint
提高FE_INEXACT
是域错误。
如math_errhandling中指定的,FE_INEXACT
可能(但不要求在非IEEE浮点平台上)rint
在舍入非整数有限值时引发。
之间唯一的区别rint
和nearbyint
是nearbyint
永远不会提高FE_INEXACT
。
最大的可表示浮点值是所有标准浮点格式中的精确整数,因此rint
不会自行溢出; 但是intmax_t
,当存储在整数变量中时,结果可能会溢出任何整数类型(包括)。
如果当前舍入模式是...
FE_DOWNWARD
,那么rint
相当于floor
。
FE_UPWARD
,那么rint
相当于ceil
。
FE_TOWARDZERO
,那么rint
相当于trunc
FE_TONEAREST
,rint
不同之处round
在于中途情况被舍入到甚至远离零。
#include <stdio.h>#include <math.h>#include <fenv.h>#include <limits.h> int main(void){#pragma STDC FENV_ACCESS ON fesetround(FE_TONEAREST); printf("rounding to nearest (halfway cases to even):\n" "rint(+2.3) = %+.1f ", rint(2.3)); printf("rint(+2.5) = %+.1f ", rint(2.5)); printf("rint(+3.5) = %+.1f\n", rint(3.5)); printf("rint(-2.3) = %+.1f ", rint(-2.3)); printf("rint(-2.5) = %+.1f ", rint(-2.5)); printf("rint(-3.5) = %+.1f\n", rint(-3.5)); fesetround(FE_DOWNWARD); printf("rounding down: \nrint(+2.3) = %+.1f ", rint(2.3)); printf("rint(+2.5) = %+.1f ", rint(2.5)); printf("rint(+3.5) = %+.1f\n", rint(3.5)); printf("rint(-2.3) = %+.1f ", rint(-2.3)); printf("rint(-2.5) = %+.1f ", rint(-2.5)); printf("rint(-3.5) = %+.1f\n", rint(-3.5)); printf("rounding down with lrint: \nlrint(+2.3) = %ld ", lrint(2.3)); printf("lrint(+2.5) = %ld ", lrint(2.5)); printf("lrint(+3.5) = %ld\n", lrint(3.5)); printf("lrint(-2.3) = %ld ", lrint(-2.3)); printf("lrint(-2.5) = %ld ", lrint(-2.5)); printf("lrint(-3.5) = %ld\n", lrint(-3.5)); printf("lrint(-0.0) = %ld\n", lrint(-0.0)); printf("lrint(-Inf) = %ld\n", lrint(-INFINITY)); // FE_INVALID raised // error handling feclearexcept(FE_ALL_EXCEPT); printf("rint(1.1) = %.1f\n", rint(1.1)); if(fetestexcept(FE_INEXACT)) puts(" FE_INEXACT was raised"); feclearexcept(FE_ALL_EXCEPT); printf("lrint(LONG_MIN-2048.0) = %ld\n", lrint(LONG_MIN-2048.0)); if(fetestexcept(FE_INVALID)) puts(" FE_INVALID was raised");}
可能的输出:
rounding to nearest (halfway cases to even):rint(+2.3) = +2.0 rint(+2.5) = +2.0 rint(+3.5) = +4.0rint(-2.3) = -2.0 rint(-2.5) = -2.0 rint(-3.5) = -4.0rounding down: rint(+2.3) = +2.0 rint(+2.5) = +2.0 rint(+3.5) = +3.0rint(-2.3) = -3.0 rint(-2.5) = -3.0 rint(-3.5) = -4.0rounding down with lrint: lrint(+2.3) = 2 lrint(+2.5) = 2 lrint(+3.5) = 3lrint(-2.3) = -3 lrint(-2.5) = -3 lrint(-3.5) = -4lrint(-0.0) = 0lrint(-Inf) = -9223372036854775808rint(1.1) = 1.0 FE_INEXACT was raisedlrint(LONG_MIN-2048.0) = -9223372036854775808 FE_INVALID was raised
C11标准(ISO / IEC 9899:2011):
7.12.9.4 rint函数(p:252)
7.12.9.5印刷和印刷功能(p:252)
7.25类型通用数学<tgmath.h>(p:373-375)
F.10.6.4 rint函数(p:527)
F.10.6.5打印和打印功能(p:527)
C99标准(ISO / IEC 9899:1999):
7.12.9.4 rint函数(p:232-233)
7.12.9.5印刷和印刷功能(p:233)
7.22类型通用数学<tgmath.h>(p:335-337)
F.9.6.4 rint函数(p:463)
F.9.6.5打印和打印功能(p:463)