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)
最も近い整数に丸め、中間の場合はゼロから離れる方向に丸める
(関数)