round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl

< c‎ | numeric‎ | math
在标头 <math.h> 定义
float       roundf( float arg );
(1) (C99 起)
double      round( double arg );
(2) (C99 起)
long double roundl( long double arg );
(3) (C99 起)
在标头 <tgmath.h> 定义
#define round( arg )
(4) (C99 起)
在标头 <math.h> 定义
long      lroundf( float arg );
(5) (C99 起)
long      lround( double arg );
(6) (C99 起)
long      lroundl( long double arg );
(7) (C99 起)
在标头 <tgmath.h> 定义
#define lround( arg )
(8) (C99 起)
在标头 <math.h> 定义
long long llroundf( float arg );
(9) (C99 起)
long long llround( double arg );
(10) (C99 起)
long long llroundl( long double arg );
(11) (C99 起)
在标头 <tgmath.h> 定义
#define llround( arg )
(12) (C99 起)
1-3) 计算与 arg 最邻近的整数值(以浮点格式),中点情况取远离零者,无关乎当前舍入模式。
5-7, 9-11) 计算与 arg 最邻近的整数值(以整数格式),中点情况取远离零者,无关乎当前舍入模式。
4,8,12) 泛型宏:若 arg 拥有 long double 类型,则分别调用 roundllroundlllroundl。否则,若 arg 拥有整数类型或 double 类型,则分别调用 roundlroundllround。否则分别调用 roundflroundfllroundf

参数

arg - 浮点值

返回值

若不出现错误,则与返回 arg 的最邻近整数值,中点情况取远离零者,

返回值
math-round away zero.svg
实参

若出现定义域错误,则返回实现定义值。

错误处理

报告 math_errhandling 中指定的错误。

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

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

对于 roundroundfroundl 函数:
  • 当前舍入模式无效。
  • arg 为 ±∞,则返回未修改的该值。
  • arg 为 ±0,则返回未修改的该值。
  • arg 为 NaN,则返回 NaN。
对于 lroundllround 函数族:
  • 决不引发 FE_INEXACT
  • 当前舍入模式无效。
  • arg 为 ±∞,则引发 FE_INVALID 并返回实现定义值。
  • 若舍入结果在返回类型的范围外,则引发 FE_INVALID 并返回实现定义值。
  • arg 为 NaN,则引发 FE_INVALID 并返回实现定义值。

注解

round 在舍入非整数有限值时,可以(但不要求)引发 FE_INEXACT

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

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

rounddouble 版本如同实现如下:

#include <math.h>
double round(double x)
{
    return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5);
}

示例

#include <assert.h>
#include <fenv.h>
#include <float.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
// #pragma STDC FENV_ACCESS ON
 
double custom_round(double x)
{
    return signbit(x) ? ceil(x - 0.5) : floor(x + 0.5);
}
 
void test_custom_round()
{
    const double sample[] =
    {
        0.0, 2.3, 2.5 - DBL_EPSILON, 2.5, 2.5 + DBL_EPSILON, 2.7, INFINITY
    };
    for (size_t t = 0; t < sizeof sample / sizeof(double); ++t)
        assert(round(+sample[t]) == custom_round(+sample[t]) &&
               round(-sample[t]) == custom_round(-sample[t]));
}
 
int main(void)
{
    // round
    printf("round(+2.3) = %+.1f  ", round(2.3));
    printf("round(+2.5) = %+.1f  ", round(2.5));
    printf("round(+2.7) = %+.1f\n", round(2.7));
    printf("round(-2.3) = %+.1f  ", round(-2.3));
    printf("round(-2.5) = %+.1f  ", round(-2.5));
    printf("round(-2.7) = %+.1f\n", round(-2.7));
 
    printf("round(-0.0) = %+.1f\n", round(-0.0));
    printf("round(-Inf) = %+f\n",   round(-INFINITY));
 
    test_custom_round();
 
    // lround
    printf("lround(+2.3) = %+ld  ", lround(2.3));
    printf("lround(+2.5) = %+ld  ", lround(2.5));
    printf("lround(+2.7) = %+ld\n", lround(2.7));
    printf("lround(-2.3) = %+ld  ", lround(-2.3));
    printf("lround(-2.5) = %+ld  ", lround(-2.5));
    printf("lround(-2.7) = %+ld\n", lround(-2.7));
 
    printf("lround(-0.0) = %+ld\n", lround(-0.0));
    printf("lround(-Inf) = %+ld\n", lround(-INFINITY)); // 引发 FE_INVALID
 
    // 错误处理
    feclearexcept(FE_ALL_EXCEPT);
    printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX + 1.5));
    if (fetestexcept(FE_INVALID))
        puts("    引发了 FE_INVALID");
}

可能的输出:

round(+2.3) = +2.0  round(+2.5) = +3.0  round(+2.7) = +3.0
round(-2.3) = -2.0  round(-2.5) = -3.0  round(-2.7) = -3.0
round(-0.0) = -0.0
round(-Inf) = -inf
lround(+2.3) = +2  lround(+2.5) = +3  lround(+2.7) = +3
lround(-2.3) = -2  lround(-2.5) = -3  lround(-2.7) = -3
lround(-0.0) = +0
lround(-Inf) = -9223372036854775808
lround(LONG_MAX+1.5) = -9223372036854775808
    引发了 FE_INVALID

引用

  • C23 标准(ISO/IEC 9899:2024):
  • 7.12.9.6 The round functions (第 TBD 页)
  • 7.12.9.7 The lround and llround functions (第 TBD 页)
  • 7.25 Type-generic math <tgmath.h> (第 TBD 页)
  • F.10.6.6 The round functions (第 TBD 页)
  • F.10.6.7 The lround and llround functions (第 TBD 页)
  • C17 标准(ISO/IEC 9899:2018):
  • 7.12.9.6 The round functions (第 184 页)
  • 7.12.9.7 The lround and llround functions (第 184-185 页)
  • 7.25 Type-generic math <tgmath.h> (第 272-273 页)
  • F.10.6.6 The round functions (第 384 页)
  • F.10.6.7 The lround and llround functions (第 385 页)
  • C11 标准(ISO/IEC 9899:2011):
  • 7.12.9.6 The round functions (第 253 页)
  • 7.12.9.7 The lround and llround functions (第 253 页)
  • 7.25 Type-generic math <tgmath.h> (第 373-375 页)
  • F.10.6.6 The round functions (第 527 页)
  • F.10.6.7 The lround and llround functions (第 528 页)
  • C99 标准(ISO/IEC 9899:1999):
  • 7.12.9.6 The round functions (第 233 页)
  • 7.12.9.7 The lround and llround functions (第 234 页)
  • 7.22 Type-generic math <tgmath.h> (第 335-337 页)
  • F.9.6.6 The round functions (第 464 页)
  • F.9.6.7 The lround and llround functions (第 464 页)

参阅

(C99)(C99)
计算不大于给定值的最大整数
(函数)
(C99)(C99)
计算不小于给定值的最小整数
(函数)
(C99)(C99)(C99)
取整到绝对值不大于给定值的最接近整数
(函数)