ldexp, ldexpf, ldexpl
提供: cppreference.com
ヘッダ <math.h> で定義 | ||
float ldexpf(float arg, intexp); | (1) | (C99以上) |
double ldexp(double arg, intexp); | (2) | |
longdouble ldexpl(longdouble arg, intexp); | (3) | (C99以上) |
ヘッダ <tgmath.h> で定義 | ||
#define ldexp( arg, exp ) | (4) | (C99以上) |
1-3) 浮動小数点値
arg
に 2
の exp
乗を掛けます。4) 型総称マクロ。
arg
が longdouble 型の場合は ldexpl
が呼ばれます。 そうでなく、 arg
が整数型または double 型の場合は ldexp
が呼ばれます。 そうでなければ ldexpf
が呼ばれます。目次 |
[編集]引数
arg | - | 浮動小数点値 |
exp | - | 整数値 |
[編集]戻り値
エラーが発生しなければ、 2 の exp
乗を掛けた arg
(arg×2exp
) が返されます。
オーバーフローによる値域エラーが発生した場合、 ±HUGE_VAL
、 ±HUGE_VALF
または ±HUGE_VALL
が返されます。
アンダーフローによる値域エラーが発生した場合、 (丸めた後の) 正しい値が返されます。
[編集]エラー処理
math_errhandling で規定されている通りにエラーが報告されます。
処理系が IEEE 浮動小数点算術 (IEC 60559) をサポートしている場合、
- 値域エラーが発生しなければ、 FE_INEXACT が発生することはありません (結果は正確です)
- 値域エラーが発生しなければ、現在の丸めモードは無視されます。
arg
が ±0 であれば、それが変更されずに返されます。arg
が ±∞ であれば、それが変更されずに返されます。exp
が 0 であれば、arg
が変更されずに返されます。arg
が NaN であれば、 NaN が返されます。
[編集]ノート
2進数 (FLT_RADIX が 2
) のシステムでは、 ldexp
は scalbn と同等です。
関数 ldexp
("load exponent") は、対となる frexp と共に、直接ビット操作を行わずに浮動小数点数表現を操作するために使用することができます。
多くの処理系では、 ldexp
は算術演算子を使用して2の乗数を乗算または除算するよりも非効率的です。
[編集]例
Run this code
#include <stdio.h>#include <math.h>#include <float.h>#include <errno.h>#include <fenv.h>#pragma STDC FENV_ACCESS ONint 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 valuesprintf("ldexp(-0, 10) = %f\n", ldexp(-0.0, 10));printf("ldexp(-Inf, -1) = %f\n", ldexp(-INFINITY, -1));//error handlingerrno=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
[編集]参考文献
- C11 standard (ISO/IEC 9899:2011):
- 7.12.6.6 The ldexp functions (p: 244)
- 7.25 Type-generic math <tgmath.h> (p: 373-375)
- F.10.3.6 The ldexp functions (p: 522)
- C99 standard (ISO/IEC 9899:1999):
- 7.12.6.6 The ldexp functions (p: 225)
- 7.22 Type-generic math <tgmath.h> (p: 335-337)
- F.9.3.6 The ldexp functions (p: 459)
- C89/C90 standard (ISO/IEC 9899:1990):
- 4.5.4.3 The ldexp function
[編集]関連項目
(C99)(C99) | 数値を仮数と 2 の指数に分解します (関数) |
(C99)(C99)(C99)(C99)(C99)(C99) | 数値と FLT_RADIX の累乗の乗算を効率良く計算します (関数) |
ldexp の C++リファレンス |