C
数值 | Numerics

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 doublerintllrintlllrintl被调用。否则,如果arg有整数类型或类型doublerintlrintllrint被调用。否则rintflrintfllrintf叫,分别。

参数

arg-浮点值

返回值

如果没有发生错误,则返回arg根据当前舍入模式最接近的整数值。

错误处理

按照math_errhandling中的指定报告错误。

如果返回类型表示的范围lrint或结果llrint超出范围,则可能会出现域错误或范围错误。

如果实现支持IEEE浮点运算(IEC 60559),则对于rint函数:

  • 如果arg是±∞,则返回,未修改

lrintllrint功能:

  • 如果arg是±∞,FE_INVALID则引发并返回实现定义的值

笔记

POSIX规定是,所有病例lrintllrint提高FE_INEXACT是域错误。

如math_errhandling中指定的,FE_INEXACT可能(但不要求在非IEEE浮点平台上)rint在舍入非整数有限值时引发。

之间唯一的区别rintnearbyintnearbyint永远不会提高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):