函式
<cmath> <ctgmath>

exp2

     double exp2  (double x);      float exp2f (float x);long double exp2l (long double x);
     double exp2 (double x);      float exp2 (float x);long double exp2 (long double x);     double exp2 (T x);           // additional overloads for integral types
計算二進位制指數函式
返回 x以 2 為底的指數函式,即 2x 次冪:2x

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

引數

x
指數的值。

返回值

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

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

示例

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

int main ()
{
  double param, result;
  param = 8.0;
  result = exp2 (param);
  printf ("2 ^ %f = %f.\n", param, result );
  return 0;
}

輸出

2 ^ 8.000000 is 256.000000.


另見