round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl
|
ヘッダーで定義
<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以降) |
roundl
、
lroundl
、
llroundl
が呼び出される。そうでなく、
arg
が整数型または
double
型の場合、
round
、
lround
、
llround
が呼び出される。それ以外の場合、それぞれ
roundf
、
lroundf
、
llroundf
が呼び出される。
目次 |
パラメータ
| arg | - | 浮動小数点値 |
戻り値
エラーが発生しない場合、 arg に最も近い整数値(中間値はゼロから遠ざかる方向に丸め)が返されます。
定義域エラーが発生した場合、実装定義の値が返されます。
エラーハンドリング
エラーは
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 <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関数群 (p: 253)
-
- 7.12.9.7 lroundおよびllround関数 (p: 253)
-
- 7.25 型総称数学 <tgmath.h> (p: 373-375)
-
- F.10.6.6 round関数群 (p: 527)
-
- F.10.6.7 lroundおよびllround関数 (p: 528)
- C99規格 (ISO/IEC 9899:1999):
-
- 7.12.9.6 round関数群 (p: 233)
-
- 7.12.9.7 lroundおよびllround関数 (p: 234)
-
- 7.22 総称数学 <tgmath.h> (p: 335-337)
-
- F.9.6.6 round関数群 (p: 464)
-
- F.9.6.7 lroundおよびllround関数 (p: 464)
関連項目
|
(C99)
(C99)
|
指定された値以下の最大の整数を計算する
(関数) |
|
(C99)
(C99)
|
指定された値以上の最小の整数を計算する
(関数) |
|
(C99)
(C99)
(C99)
|
指定された値の絶対値以下の最も近い整数に丸める
(関数) |
|
C++ documentation
for
round
|
|