ldexp, ldexpf, ldexpl
From cppreference.com
| ヘッダー <math.h> で定義 |
||
| float ldexpf( float arg, int exp ); |
(1) | (C99以降) |
| double ldexp( double arg, int exp ); |
(2) | |
| long double ldexpl( long double arg, int exp ); |
(3) | (C99以降) |
| ヘッダー <tgmath.h> で定義 |
||
| #define ldexp( arg, exp ) |
(4) | (C99以降) |
4) 型汎用マクロ: arg の型が long double の場合、
ldexpl が呼び出されます。それ以外の場合で arg の型が整数型または double の場合、ldexp が呼び出されます。それ以外の場合、それぞれ ldexpf が呼び出されます。目次 |
[edit] パラメータ
| arg | - | floating-point value |
| exp | - | 整数値 |
[edit] 戻り値
エラーが発生しない場合、arg に 2 の exp 乗を掛けた値(arg×2exp
)が返されます。
オーバーフローによる範囲エラーが発生した場合、±HUGE_VAL、±HUGE_VALF、または ±HUGE_VALL が返されます。
アンダーフローによる範囲エラーが発生した場合、正しい結果(丸め後)が返されます。
[edit] エラー処理
エラーは math_errhandling で指定されたとおりに報告されます。
実装がIEEE浮動小数点算術 (IEC 60559) をサポートしている場合、
- 範囲エラーが発生しない限り、FE_INEXACT は決して発生しません(結果は正確です)。
- 範囲エラーが発生しない限り、現在の丸めモードは無視されます。
- arg が ±0 の場合、変更されずに返されます。
- arg が ±∞ の場合、変更されずに返されます。
- exp が 0 の場合、arg は変更されずに返されます。
- arg が NaN の場合、NaN が返されます。
[edit] 注記
バイナリシステム(FLT_RADIX が 2 の場合)では、ldexp は scalbn と同等です。
関数 ldexp("load exponent"、「指数をロードする」)は、その双対である frexp とともに、直接的なビット操作なしに浮動小数点数の表現を操作するために使用できます。
多くの実装では、ldexp は算術演算子を使用した2のべき乗による乗算または除算よりも効率が悪いです。
[edit] 例
このコードを実行
#include <errno.h> #include <fenv.h> #include <float.h> #include <math.h> #include <stdio.h> // #pragma STDC FENV_ACCESS ON int main(void) { printf("ldexp(7, -4) = %f\n", ldexp(7, -4)); printf("ldexp(1, -1074) = %g (minimum positive subnormal double)\n", ldexp(1, -1074)); printf("ldexp(nextafter(1,0), 1024) = %g (largest finite double)\n", ldexp(nextafter(1,0), 1024)); // special values printf("ldexp(-0, 10) = %f\n", ldexp(-0.0, 10)); printf("ldexp(-Inf, -1) = %f\n", ldexp(-INFINITY, -1)); // error handling errno = 0; feclearexcept(FE_ALL_EXCEPT); printf("ldexp(1, 1024) = %f\n", ldexp(1, 1024)); if (errno == ERANGE) perror(" errno == ERANGE"); if (fetestexcept(FE_OVERFLOW)) puts(" FE_OVERFLOW raised"); }
実行結果の例
ldexp(7, -4) = 0.437500
ldexp(1, -1074) = 4.94066e-324 (minimum positive subnormal double)
ldexp(nextafter(1,0), 1024) = 1.79769e+308 (largest finite double)
ldexp(-0, 10) = -0.000000
ldexp(-Inf, -1) = -inf
ldexp(1, 1024) = inf
errno == ERANGE: Numerical result out of range
FE_OVERFLOW raised[edit] 参考文献
- C23標準 (ISO/IEC 9899:2024)
- 7.12.6.6 The ldexp functions (p: TBD)
- 7.25 Type-generic math <tgmath.h> (p: TBD)
- F.10.3.6 The ldexp functions (p: TBD)
- C17標準 (ISO/IEC 9899:2018)
- 7.12.6.6 The ldexp functions (p: TBD)
- 7.25 Type-generic math <tgmath.h> (p: TBD)
- F.10.3.6 The ldexp functions (p: TBD)
- C11標準 (ISO/IEC 9899:2011)
- 7.12.6.6 The ldexp functions (p: 244)
- 7.25 型総称数学関数 <tgmath.h> (p: 373-375)
- F.10.3.6 The ldexp functions (p: 522)
- C99標準 (ISO/IEC 9899:1999)
- 7.12.6.6 The ldexp functions (p: 225)
- 7.22 型総称数学関数 <tgmath.h> (p: 335-337)
- F.9.3.6 The ldexp functions (p: 459)
- C89/C90標準 (ISO/IEC 9899:1990)
- 4.5.4.3 The ldexp function
[edit] 関連項目
| (C99)(C99) |
数を仮数部と2のべき乗に分割する (関数) |
| (C99)(C99)(C99)(C99)(C99)(C99) |
FLT_RADIXのべき乗に数を効率的に掛ける (関数) |
| C++ ドキュメント ( ldexp)
| |