rintf
rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl
在头文件 | | |
---|---|---|
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
是±∞,则返回,未修改
为lrint
和llrint
功能:
- 如果
arg
是±∞,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
。
例
#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):