Namespaces
Variants

ldexp, ldexpf, ldexpl

From cppreference.net
< c ‎ | numeric ‎ | math
Common mathematical functions
Functions
Basic operations
(C99)
(C99)
(C99)
(C99) (C99) (C99) (C23)
Maximum/minimum operations
Exponential functions
Power functions
Trigonometric and hyperbolic functions
Nearest integer floating-point
(C99) (C99) (C99)
(C23) (C23) (C23) (C23)
Floating-point manipulation
ldexp
(C99) (C99)
(C99) (C23)
(C99)
Narrowing operations
(C23)
(C23)
(C23)
(C23)
(C23)
(C23)
Quantum and quantum exponent
Decimal re-encoding functions
Total order and payload functions
Classification
Error and gamma functions
(C99)
(C99)
(C99)
(C99)
Types
Macro constants
Special floating-point values
Arguments and return values
Error handling
Fast operation indicators
定義先ヘッダ <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以降)
1-3) 浮動小数点値 arg を数値 2 exp 乗で乗算します。
4) 型総称マクロ: arg の型が long double の場合、 ldexpl が呼び出される。それ以外の場合、 arg が整数型または double 型の場合、 ldexp が呼び出される。それ以外の場合、それぞれ ldexpf が呼び出される。

目次

パラメータ

arg - 浮動小数点値
exp - 整数値

戻り値

エラーが発生しない場合、 arg に2の exp 乗を乗算した値( arg×2 exp
)が返されます。

オーバーフローによる範囲エラーが発生した場合、 ±HUGE_VAL ±HUGE_VALF または ±HUGE_VALL が返されます。

アンダーフローによる範囲エラーが発生した場合、正しい結果(丸め後)が返されます。

エラー処理

エラーは math_errhandling で指定された通りに報告されます。

IEEE浮動小数点演算(IEC 60559)を実装がサポートしている場合、

  • 範囲エラーが発生しない限り、 FE_INEXACT は決して発生しない(結果は正確)
  • 範囲エラーが発生しない限り、 現在の丸めモード は無視される
  • arg が±0の場合、変更されずに返される
  • arg が±∞の場合、変更されずに返される
  • exp が0の場合、 arg は変更されずに返される
  • arg がNaNの場合、NaNが返される

注記

バイナリシステム( FLT_RADIX 2 の場合)では、 ldexp scalbn と等価です。

ldexp 関数(「指数部のロード」)は、その双対である frexp と共に、浮動小数点数の表現を直接的なビット操作なしで操作するために使用できます。

多くの実装では、 ldexp は算術演算子を用いた2のべき乗による乗算や除算よりも効率が劣ります。

#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

参考文献

  • C23規格 (ISO/IEC 9899:2024):
  • 7.12.6.6 ldexp関数群 (p: 未定)
  • 7.25 総称数学 <tgmath.h> (p: 未定)
  • F.10.3.6 ldexp関数群 (p: 未定)
  • C17規格 (ISO/IEC 9899:2018):
  • 7.12.6.6 ldexp関数 (p: TBD)
  • 7.25 総称型数学 <tgmath.h> (p: TBD)
  • F.10.3.6 ldexp関数 (p: TBD)
  • C11規格 (ISO/IEC 9899:2011):
  • 7.12.6.6 ldexp関数 (p: 244)
  • 7.25 総称数学 <tgmath.h> (p: 373-375)
  • F.10.3.6 ldexp関数 (p: 522)
  • C99規格 (ISO/IEC 9899:1999):
  • 7.12.6.6 ldexp関数 (p: 225)
  • 7.22 総称数学 <tgmath.h> (p: 335-337)
  • F.9.3.6 ldexp関数 (p: 459)
  • C89/C90標準 (ISO/IEC 9899:1990):
  • 4.5.4.3 ldexp関数

関連項目

数値を仮数と2の累乗に分解する 2
(関数)
(C99) (C99) (C99) (C99) (C99) (C99)
数値に FLT_RADIX の累乗を効率的に乗算する
(関数)