round, roundf, roundl, lround, lroundf, lroundl, llround, llroundf, llroundl
From cppreference.com
| ヘッダー <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 の最も近い整数値を、半数 cases をゼロから離れる方向に丸めて計算します。現在の丸めモードには影響されません。
5-7, 9-11) arg の最も近い整数値を、半数 cases をゼロから離れる方向に丸めて計算します。現在の丸めモードには影響されません。
4,8,12) 型汎用マクロ: arg の型が long double の場合、`roundl`、`lroundl`、`llroundl` が呼び出されます。そうでなく、arg の型が整数型または double の場合、`round`、`lround`、`llround` が呼び出されます。それ以外の場合、それぞれ `roundf`、`lroundf`、`llroundf` が呼び出されます。
目次 |
[edit] パラメータ
| arg | - | floating-point value |
[edit] 戻り値
エラーが発生しない場合、arg の最も近い整数値が、半数 cases をゼロから離れる方向に丸めて返されます。
戻り値
引数
ドメインエラーが発生した場合、実装定義の値が返されます。
[edit] エラー処理
エラーは 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 が発生し、実装定義の値が返されます。
[edit] 注記
非整数有限値を丸める際に、`round` は FE_INEXACT を発生させる場合があります(必須ではありません)。
すべての標準浮動小数点形式において、表現可能な最大の浮動小数点値は正確な整数であるため、`round` 自体でオーバーフローすることはありません。ただし、整数変数に格納する際には、結果が(intmax_t を含む)任意の整数型をオーバーフローする可能性があります。
POSIX では、`lround` または `llround` が FE_INVALID を発生させるすべてのケースはドメインエラーであると規定されています。
double 版の `round` は、次のように実装されているかのように動作します。
[edit] 例
このコードを実行
#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[edit] 参考文献
- C23標準 (ISO/IEC 9899:2024)
- 7.12.9.6 The round functions (p: TBD)
- 7.12.9.7 The lround and llround functions (p: TBD)
- 7.25 Type-generic math <tgmath.h> (p: TBD)
- F.10.6.6 The round functions (p: TBD)
- F.10.6.7 The lround and llround functions (p: TBD)
- C17標準 (ISO/IEC 9899:2018)
- 7.12.9.6 The round functions (p: 184)
- 7.12.9.7 The lround and llround functions (p: 184-185)
- 7.25 型総称数学関数 <tgmath.h> (p: 272-273)
- F.10.6.6 The round functions (p: 384)
- F.10.6.7 The lround and llround functions (p: 385)
- C11標準 (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 型総称数学関数 <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標準 (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 型総称数学関数 <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) |
与えられた値以下の最大の整数を計算する (関数) |
| (C99)(C99) |
与えられた値以上の最小の整数を計算する (関数) |
| (C99)(C99)(C99) |
与えられた値の絶対値より大きくない最も近い整数に丸める (関数) |
| C++ ドキュメント (`round` について)
| |