名前空間
変種
操作

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

提供: cppreference.com
< c‎ | numeric‎ | math
 
 
 
一般的な数学関数
関数
基本操作
(C99)
(C99)
(C99)
(C99)
(C99)
(C99)(C99)(C99)
指数関数
(C99)
(C99)
(C99)
(C99)
冪関数
(C99)
(C99)
三角関数と双曲線関数
(C99)
(C99)
(C99)
誤差関数とガンマ関数
(C99)
(C99)
(C99)
(C99)
最も近い整数
roundlroundllround
(C99)(C99)(C99)
(C99)
(C99)(C99)(C99)
浮動小数点操作関数
(C99)(C99)
(C99)
(C99)
分類
(C99)
(C99)
(C99)
(C99)(C99)
マクロ定数
 
ヘッダ <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) 型総称マクロ。 arglong 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 が発生し、処理系定義の値が返されます。

[編集] ノート

非整数の有限な値を丸めるとき、 round によって FE_INEXACT が発生するかもしれません (が要求されているわけではありません)。

すべての標準の浮動小数点フォーマットにおいて、最も大きな表現可能な浮動小数点値は正確な整数であるため、 round それ自体はオーバーフローすることはありません。 しかし任意の整数型 (intmax_t も含みます) は、整数変数に格納するとき、結果がオーバーフローするかもしれません。

POSIX は、 lround または llroundFE_INEXACT を発生するすべてのケースは定義域エラーであると規定しています

rounddouble 版は以下のように実装されているかのように動作します。

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

[編集]

#include <stdio.h>
#include <math.h>
#include <fenv.h>
#include <limits.h>
 
#pragma STDC FENV_ACCESS ON
 
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));
 
    // 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 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

[編集] 参考文献

  • C11 standard (ISO/IEC 9899:2011):
  • 7.12.9.6 The round functions (p: 253)
  • 7.12.9.7 The lround and llround functions (p: 253)
  • 7.25 Type-generic math <tgmath.h> (p: 373-375)
  • F.10.6.6 The round functions (p: 527)
  • F.10.6.7 The lround and llround functions (p: 528)
  • C99 standard (ISO/IEC 9899:1999):
  • 7.12.9.6 The round functions (p: 233)
  • 7.12.9.7 The lround and llround functions (p: 234)
  • 7.22 Type-generic math <tgmath.h> (p: 335-337)
  • F.9.6.6 The round functions (p: 464)
  • F.9.6.7 The lround and llround functions (p: 464)

[編集] 関連項目

指定された値より大きくない最も大きな整数を返します
(関数) [edit]
(C99)(C99)
指定された値より小さくない最も小さな整数を返します
(関数) [edit]
(C99)(C99)(C99)
指定された値より絶対値が大きくない最も近い整数に丸めます
(関数) [edit]