lround, lroundf, lroundl, llround, llroundf, llroundl

将浮点值舍入为最接近的整数。

语法

long lround(
   double x
);
long lround(
   float x
);  // C++ only
long lround(
   long double x
);  // C++ only
long lroundf(
   float x
);
long lroundl(
   long double x
);
long long llround(
   double x
);
long long llround(
   float x
);  // C++ only
long long llround(
   long double x
);  // C++ only
long long llroundf(
   float x
);
long long llroundl(
   long double x
);
#define lround(X) // Requires C11 or higher

参数

x
要舍入的浮点值。

返回值

lroundllround 函数返回最接近 xlonglong long 整数。 中间值从零舍入,这与浮点舍入模式的设置无关。 无错误返回。

输入 SEH 异常 _matherr 异常
± QNaN、IND _DOMAIN

备注

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

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

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

要求

例程 必需的标头
lround, lroundf, lroundl, llround, llroundf, llroundl <math.h>
lround <tgmath.h>

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

示例

// crt_lround.c
// Build with: cl /W4 /Tc crt_lround.c
// This example displays the rounded results of
// the floating-point values 2.499999, -2.499999,
// 2.8, -2.8, 3.5 and -3.5.

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

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

   printf("lround(%f) is %d\n", x, lround(x));
   printf("lround(%f) is %d\n", -x, lround(-x));
   printf("lroundf(%f) is %d\n", y, lroundf(y));
   printf("lroundf(%f) is %d\n", -y, lroundf(-y));
   printf("lroundl(%Lf) is %d\n", z, lroundl(z));
   printf("lroundl(%Lf) is %d\n", -z, lroundl(-z));
}
lround(2.499999) is 2
lround(-2.499999) is -2
lroundf(2.800000) is 3
lroundf(-2.800000) is -3
lroundl(3.500000) is 4
lroundl(-3.500000) is -4

另请参阅

数学和浮点支持
ceilceilfceill
floorfloorffloorl
fmodfmodf
lrintlrintflrintlllrintllrintfllrintl
roundroundfroundl
nearbyintnearbyintfnearbyintl
rintrintfrintl