rint, rintf, rintl

将浮点值舍入到最接近的整数(采用浮点格式)。

语法

double rint( double x );
float rintf( float x );
long double rintl( long double x );
#define rint(X) // Requires C11 or higher

float rint( float x );  // C++ only
long double rint( long double x );  // C++ only

参数

x
要舍入的浮点值。

返回值

rint 函数将返回表示最接近 x 的整数的浮点值。 与 nearbyint 函数相同,根据浮点舍入模式的当前设置对中间值进行舍入。 与 nearbyint 函数不同,如果结果不同于参数中的值,则 rint 函数可能会引起 FE_INEXACT 浮点异常。 无错误返回。

输入 SEH 异常 _matherr 异常
± QNAN、IND、INF
非规格化数 EXCEPTION_FLT_UNDERFLOW

备注

由于 C++ 允许重载,因此你可以调用采用并返回 rintfloat 值的 long double 重载。 在 C 程序中,除非使用 <tgmath.h> 宏调用此函数,否则 rint 始终采用并返回 double

如果使用 <tgmath.h>rint() 宏,则参数的类型将决定选择哪个版本的函数。 有关详细信息,请参阅泛型类型数学

默认情况下,此函数的全局状态范围限定为应用程序。 若要更改此行为,请参阅 CRT 中的全局状态

要求

功能 C 标头 C++ 标头
rint, rintf, rintl <math.h> <cmath>
rint <tgmath.h>

有关兼容性的详细信息,请参阅 兼容性

示例

// crt_rint.c
// Build with: cl /W3 /Tc crt_rint.c
// This example displays the rounded results of
// the floating-point values 2.499999, -2.499999,
// 2.8, -2.8, 2.5 and -2.5.

#include <math.h>
#include <stdio.h>

int main( void )
{
   double x = 2.499999;
   float y = 2.8f;
   long double z = 2.5;

   printf("rint(%f) is %.0f\n", x, rint (x));
   printf("rint(%f) is %.0f\n", -x, rint (-x));
   printf("rintf(%f) is %.0f\n", y, rintf(y));
   printf("rintf(%f) is %.0f\n", -y, rintf(-y));
   printf("rintl(%Lf) is %.0Lf\n", z, rintl(z));
   printf("rintl(%Lf) is %.0Lf\n", -z, rintl(-z));
}
rint(2.499999) is 2
rint(-2.499999) is -2
rintf(2.800000) is 3
rintf(-2.800000) is -3
rintl(2.500000) is 3
rintl(-2.500000) is -3

另请参阅

数学和浮点支持
ceilceilfceill
floorfloorffloorl
fmodfmodf
lrintlrintflrintlllrintllrintfllrintl
lroundlroundflroundlllroundllroundfllroundl
nearbyintnearbyintfnearbyintl
rint