函式
<cmath> <ctgmath>

ldexp

double ldexp (double x, int exp);
     double ldexp  (double x     , int exp);      float ldexpf (float x      , int exp);long double ldexpl (long double x, int exp);
     double ldexp (double x     , int exp);      float ldexp (float x      , int exp);long double ldexp (long double x, int exp);
     double ldexp (double x     , int exp);      float ldexp (float x      , int exp);long double ldexp (long double x, int exp);     double ldexp (T x          , int exp); // additional overloads for integral types
透過有效數和指數生成值
返回 x (有效數) 乘以 2exp (指數) 次方的結果。

lexpr(x,exp) = x * 2 exp
標頭檔案 <tgmath.h> 提供了此函式的型別通用宏版本。
此標頭檔案 (<cmath>) 為整數型別提供了額外的過載:這些過載在計算前有效地將 x 轉換為 double(為任意整數型別T 定義)。

引數

x
表示有效數的浮點值。
exp
指數的值。

返回值

函式返回

x * 2 exp

如果結果的量級太大,無法用返回型別的值表示,函式會返回帶正確符號的 HUGE_VAL(或 HUGE_VALFHUGE_VALL),併發生上溢範圍錯誤

如果發生上溢範圍錯誤,全域性變數 errno 會被設定為 ERANGE
如果發生上溢範圍錯誤
- 並且 math_errhandling 設定了 MATH_ERRNO:全域性變數 errno 被設定為 ERANGE
- 並且 math_errhandling 設定了 MATH_ERREXCEPT:將引發 FE_OVERFLOW

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* ldexp example */
#include <stdio.h>      /* printf */
#include <math.h>       /* ldexp */

int main ()
{
  double param, result;
  int n;

  param = 0.95;
  n = 4;
  result = ldexp (param , n);
  printf ("%f * 2^%d = %f\n", param, n, result);
  return 0;
}

輸出

0.950000 * 2^4 = 15.200000


另見