lrint (Numerics) – C 中文开发手册
lrint (Numerics) - C 中文开发手册
在头文件<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.0 rint(-2.3) = -2.0 rint(-2.5) = -2.0 rint(-3.5) = -4.0 rounding down: rint(+2.3) = +2.0 rint(+2.5) = +2.0 rint(+3.5) = +3.0 rint(-2.3) = -3.0 rint(-2.5) = -3.0 rint(-3.5) = -4.0 rounding down with lrint: lrint(+2.3) = 2 lrint(+2.5) = 2 lrint(+3.5) = 3 lrint(-2.3) = -3 lrint(-2.5) = -3 lrint(-3.5) = -4 lrint(-0.0) = 0 lrint(-Inf) = -9223372036854775808 rint(1.1) = 1.0 FE_INEXACT was raised lrint(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)
C 语言中文开发手册