rint, rintf, rintl, lrint, lrintf, lrintl, llrint, llrintf, llrintl

来自cppreference.com
< c‎ | numeric‎ | math
 
 
 
常用数学函数
函数
基本运算
remainder(C99)
remquo(C99)
fma(C99)
fmax(C99)
fmin(C99)
fdim(C99)
nan
nanf
nanl
(C99)
(C99)
(C99)
指数函数
exp
exp2(C99)
expm1(C99)
log
log10
log1p(C99)
log2(C99)
幂函数
sqrt
cbrt(C99)
hypot(C99)
pow
三角及双曲函数
sinh
cosh
tanh
asinh(C99)
acosh(C99)
atanh(C99)
误差及伽马函数
erf(C99)
erfc(C99)
lgamma(C99)
tgamma(C99)
临近整数的浮点运算
ceil
floor
round
lround
llround
(C99)
(C99)
(C99)
trunc(C99)
nearbyint(C99)
rint
lrint
llrint
(C99)
(C99)
(C99)
浮点数操作函数
ldexp
scalbn
scalbln
(C99)
(C99)
ilogb(C99)
logb(C99)
分类
fpclassify(C99)
isfinite(C99)
isinf(C99)
isnan(C99)
isnormal(C99)
signbit(C99)
宏常量
 
定义于头文件 <math.h>
float rintf( float arg );
(1) (C99起)
double rint( double arg );
(2) (C99起)
long double rintl( long double arg );
(3) (C99起)
定义于头文件 <tgmath.h>
#define rint( arg )
(4) (C99起)
定义于头文件 <math.h>
long lrintf( float arg );
(5) (C99起)
long lrint( double arg );
(6) (C99起)
long lrintl( long double arg );
(7) (C99起)
定义于头文件 <tgmath.h>
#define lrint( arg )
(8) (C99起)
定义于头文件 <math.h>
long long llrintf( float arg );
(9) (C99起)
long long llrint( double arg );
(10) (C99起)
long long llrintl( long double arg );
(11) (C99起)
定义于头文件 <tgmath.h>
#define llrint( arg )
(12) (C99起)
1-3) 将浮点参数arg以浮点格式取整到整数值,以当前舍入模式。
5-7, 9-11) 将浮点参数arg以整数格式取整到整数值,以当前舍入模式。
4,8,12) 通用类型宏:若arg类型为long double,则调用rintllrintlllrintl。否则若arg类型为整数或double,则调用rintlrintllrint。否则各自调用rintflrintfllrintf

目录

[编辑] 参数

arg - 浮点值

[编辑] 返回值

若无错误发生,返回根据当前舍入模式的最近的整数值。

[编辑] 错误处理

错误会按照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_errhandling所指定,FE_INEXACT可以(但不对非IEEE浮点数平台强制要求)为rint在取整有限非整数值时引发。

rintnearbyint仅有的区别是nearbyint始终不引发FE_INEXACT

最大可表示浮点值对于所有平台的浮点格式都是准确的整数,故rint自身始终不会向上溢出;然而结果可能对任何整数类型溢出(含intmax_t),在将值存储于整数变量时。

若当前舍入模式是……

  • FE_DOWNWARD,则rint等价于floor
  • FE_UPWARD,则rint等价于ceil
  • FE_TOWARDZERO,则rint等价于trunc
  • FE_TONEAREST,则rintround仅在两整数正中央时有别,后者取远离零的值。

[编辑] 示例

#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)
取整到绝对值不大于给定值的最接近整数
(函数) [edit]
用当前舍入模式取整到整数
(函数) [edit]
获得或设置舍入方向
(函数) [edit]
rintC++文档