C 庫 - exp() 函式



C 庫的 exp() 函式,型別為 double,接受單個引數 (x),返回以 e 為底 x 的指數值。

在數學中,指數是一個小的整數,位於底數的右上角。

語法

以下是 C 庫函式 exp() 的語法:

double exp(double x)

引數

此函式僅接受一個引數:

  • x - 這是浮點值。

返回值

此函式返回 x 的指數值。

示例 1

C 庫程式展示了 exp() 函式的使用方法。

#include <stdio.h>
#include <math.h>

int main () {
   double x = 0;
  
   printf("The exponential value of %lf is %lf\n", x, exp(x));
   printf("The exponential value of %lf is %lf\n", x+1, exp(x+1));
   printf("The exponential value of %lf is %lf\n", x+2, exp(x+2));
   
   return(0);
}

輸出

執行上述程式碼後,我們得到以下結果:

The exponential value of 0.000000 is 1.000000
The exponential value of 1.000000 is 2.718282
The exponential value of 2.000000 is 7.389056

示例 2

下面的程式演示了在 for 迴圈中使用 exp()。

#include <stdio.h>
#include <math.h>

int main() {
   printf("Exponential Series (e^x) for x from 1 to 10:\n");
   for (int x = 1; x <= 10; ++x) {
       double result = exp(x);
       printf("e^%d = %.6lf\n", x, result);
   }

   return 0;
}

輸出

執行上述程式碼後,我們得到以下結果:

Exponential Series (e^x) for x from 1 to 10:
e^1 = 2.718282
e^2 = 7.389056
e^3 = 20.085537
e^4 = 54.598150
e^5 = 148.413159
e^6 = 403.428793
e^7 = 1096.633158
e^8 = 2980.957987
e^9 = 8103.083928
e^10 = 22026.465795

示例 3

在此程式中,它說明了如何使用 exp() 來查詢 (e^x),其中 (x) 是任何實數。

#include <stdio.h>
#include <math.h>

int main() {
   double x = 3.0;
   double result = exp(x);

   printf("e raised to the power %.2lf = %.2lf\n", x, result);
   return 0;
}

輸出

上述程式碼產生以下結果:

e raised to the power 3.00 = 20.09
廣告