Namespaces
Variants

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

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
round lround llround
(C99) (C99) (C99)
(C99)

(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定义于头文件 <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 类型,则调用 roundl lroundl llroundl 。否则,若 arg 具有整数类型或 double 类型,则调用 round lround llround 。否则,将分别调用 roundf lroundf llroundf

目录

参数

arg - 浮点数值

返回值

如果没有发生错误,将返回最接近 arg 的整数值,四舍五入时中间值向远离零的方向取整。

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

如果发生定义域错误,则返回一个实现定义的值。

错误处理

错误报告方式遵循 math_errhandling 中的规范。

如果 lround llround 的结果超出返回类型可表示的范围,则可能发生定义域错误或范围错误。

如果实现支持 IEEE 浮点算术 (IEC 60559):

对于 round roundf roundl 函数:
  • 当前 舍入模式 不产生影响。
  • arg 为 ±∞,则直接返回原值。
  • arg 为 ±0,则直接返回原值。
  • arg 为 NaN,则返回 NaN。
对于 lround llround 函数族:
  • 永远不会引发 FE_INEXACT 异常。
  • 当前 舍入模式 不产生影响。
  • arg 为 ±∞,将引发 FE_INVALID 异常并返回实现定义的值。
  • 若舍入结果超出返回类型的取值范围,将引发 FE_INVALID 异常并返回实现定义的值。
  • arg 为 NaN,将引发 FE_INVALID 异常并返回实现定义的值。

注释

FE_INEXACT 可能在(但不要求必须)对非整数有限值进行舍入时由 round 函数引发。

在所有标准浮点格式中,最大可表示的浮点值都是精确整数,因此 round 函数自身永远不会溢出;然而当结果存入整型变量时,可能会溢出任何整数类型(包括 intmax_t )。

POSIX 规范 明确规定,所有导致 lround llround 引发 FE_INVALID 的情况均属于定义域错误。

double 版本的 round 函数行为类似于以下实现方式:

#include <math.h>
#pragma STDC FENV_ACCESS ON
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 raised
    // error handling
    feclearexcept(FE_ALL_EXCEPT);
    printf("lround(LONG_MAX+1.5) = %ld\n", lround(LONG_MAX + 1.5));
    if (fetestexcept(FE_INVALID))
        puts("    FE_INVALID was raised");
}

可能的输出:

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 was raised

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.9.6 round 函数集 (p: TBD)
  • 7.12.9.7 lround 和 llround 函数 (p: TBD)
  • 7.25 泛型数学 <tgmath.h> (p: TBD)
  • F.10.6.6 round 函数集 (p: TBD)
  • F.10.6.7 lround 和 llround 函数 (p: TBD)
  • C17 标准 (ISO/IEC 9899:2018):
  • 7.12.9.6 round 函数集 (p: 184)
  • 7.12.9.7 lround 和 llround 函数 (p: 184-185)
  • 7.25 泛型数学 <tgmath.h> (p: 272-273)
  • F.10.6.6 round 函数集 (p: 384)
  • F.10.6.7 lround 和 llround 函数 (p: 385)
  • C11标准(ISO/IEC 9899:2011):
  • 7.12.9.6 round函数族(第253页)
  • 7.12.9.7 lround和llround函数(第253页)
  • 7.25 泛型数学 <tgmath.h>(第373-375页)
  • F.10.6.6 round函数族(第527页)
  • F.10.6.7 lround和llround函数(第528页)
  • C99标准(ISO/IEC 9899:1999):
  • 7.12.9.6 round函数集(页码:233)
  • 7.12.9.7 lround与llround函数集(页码:234)
  • 7.22 泛型数学 <tgmath.h>(页码:335-337)
  • F.9.6.6 round函数集(页码:464)
  • F.9.6.7 lround与llround函数集(页码:464)

参见

计算不大于给定值的最大整数
(函数)
(C99) (C99)
计算不小于给定值的最小整数
(函数)
(C99) (C99) (C99)
向绝对值不大于给定值的最近整数取整
(函数)
C++ 文档 中关于 round 的内容