Namespaces
Variants

roundeven, roundevenf, roundevenl

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
(C99) (C99) (C99)
roundeven
(C23)
(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 roundevenf ( float arg ) ;
(1) (C23 起)
double roundeven ( double arg ) ;
(2) (C23 起)
long double roundevenl ( long double arg ) ;
(3) (C23 起)
定义于头文件 <tgmath.h>
#define roundeven( arg )
(4) (C23 起)
1-3) 计算最接近 arg (以浮点格式表示)的整数值,将中间情况向最近的偶数取整,与当前舍入模式无关。
4) 类型泛型宏:若 arg 具有 long double 类型,则调用 roundevenl 。否则,若 arg 具有整数类型或 double 类型,则调用 roundeven 。否则,分别调用 roundevenf

目录

参数

arg - 浮点数值

返回值

如果未发生错误,则返回最接近 arg 的整数值,四舍五入到最接近的偶数整数。

错误处理

此函数不受 math_errhandling 中指定的任何错误影响。

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

  • FE_INEXACT 永不触发。
  • arg 为 ±∞,则直接返回该值。
  • arg 为 ±0,则直接返回该值。
  • arg 为 NaN,则返回 NaN。

示例

#include <math.h>
#include <stdio.h>
int main(void)
{
    printf("roundeven(+2.4) = %+.1f\n", roundeven(2.4));
    printf("roundeven(-2.4) = %+.1f\n", roundeven(-2.4));
    printf("roundeven(+2.5) = %+.1f\n", roundeven(2.5));
    printf("roundeven(-2.5) = %+.1f\n", roundeven(-2.5));
    printf("roundeven(+2.6) = %+.1f\n", roundeven(2.6));
    printf("roundeven(-2.6) = %+.1f\n", roundeven(-2.6));
    printf("roundeven(+3.5) = %+.1f\n", roundeven(3.5));
    printf("roundeven(-3.5) = %+.1f\n", roundeven(-3.5));
    printf("roundeven(-0.0) = %+.1f\n", roundeven(-0.0));
    printf("roundeven(-Inf) = %+f\n",   roundeven(-INFINITY));
}

可能的输出:

roundeven(+2.4) = +2.0
roundeven(-2.4) = -2.0
roundeven(+2.5) = +2.0
roundeven(-2.5) = -2.0
roundeven(+2.6) = +3.0
roundeven(-2.6) = -3.0
roundeven(+3.5) = +4.0
roundeven(-3.5) = -4.0
roundeven(-0.0) = -0.0
roundeven(-Inf) = -inf

参考文献

  • C23 标准 (ISO/IEC 9899:2024):
  • 7.12.9.8 roundeven 函数集 (p: 265-266)
  • 7.27 泛型数学 <tgmath.h> (p: 386-390)
  • F.10.6.8 roundeven 函数集 (p: 532)

参见

(C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99)
使用当前舍入模式将数值舍入为整数,
若结果不同则产生异常
(函数)
(C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99) (C99)
舍入到最接近的整数,中间情况向远离零的方向舍入
(函数)