C 参考手册

位置:首页 > C 参考手册 >数值 >常用数学函数 > rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl

若不出现错误,则按照当前舍入模式的最接近整数值。

错误处理

报告 math_errhandling 中指定的错误。

lrintllrint 的结果在返回类型的可表示范围外,则可能出现定义域错误或值域错误。

若实现支持 IEEE 浮点算术( IEC 60559 ),则

对于 rint 函数:
  • arg 为 ±∞ ,则返回不修改的参数
  • arg 为 ±0 ,则返回不修改的参数
  • arg 为 NaN ,则返回 NaN
对于 lrintllrint 函数:
  • arg 为 ±∞ ,则引发 FE_INVALID 并返回实现定义值
  • 若舍入结果在返回类型范围外,则引发 FE_INVALID 并返回实现定义值
  • arg 为 NaN ,则引发 FE_INVALID 并返回实现定义值

注意

POSIX 指定 lrintllrint 引发 FE_INEXACT 的所有情况都是定义域错误。

如指定于 math_errhandlingrint 在舍入非整数有限值时可以(但不在非 IEEE 浮点平台上要求)引发 FE_INEXACT

rintnearbyint 间仅有的区别是 nearbyint 决不引发 FE_INEXACT

所有标准浮点格式中,最大可表示浮点值都是准确的整数,故 rint 自身决不上溢;然而在存储结果于整数对象时,结果可能溢出任何整数类型(包含 intmax_t )。

若当前舍入模式为……

示例

#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
 
    // 错误处理
    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 standard (ISO/IEC 9899:2011):
  • 7.12.9.4 The rint functions (p: 252)
  • 7.12.9.5 The lrint and llrint functions (p: 252)
  • 7.25 Type-generic math <tgmath.h> (p: 373-375)
  • F.10.6.4 The rint functions (p: 527)
  • F.10.6.5 The lrint and llrint functions (p: 527)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.12.9.4 The rint functions (p: 232-233)
  • 7.12.9.5 The lrint and llrint functions (p: 233)
  • 7.22 Type-generic math <tgmath.h> (p: 335-337)
  • F.9.6.4 The rint functions (p: 463)
  • F.9.6.5 The lrint and llrint functions (p: 463)

参阅

(C99)(C99)(C99)
取整到绝对值不大于给定值的最接近整数
(函数)
用当前舍入模式取整到整数
(函数)
获得或设置数字的舍入方向
(函数)