C 庫 - cexp() 函式



C 的複數cexp() 函式用於計算給定 z(複數)的複數以 e 為底的指數。

此函式取決於 z(複數)的型別。如果 z 為“float”型別,我們可以使用cexpf()計算以 e 為底的複數指數;對於 long double 型別,使用cexpl();對於 double 型別,使用cexp()

注意:對於 z = a+bi,複數指數函式 ez 等於 ea cis(b),其中 cis(b) 為 cos(b) + isin(b)。指數函式是複平面上的整函式,沒有分支切割。

語法

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

double complex cexp( double complex z );

引數

此函式接受一個引數:

  • Z − 它表示我們要計算其指數的複數。

返回值

如果未發生錯誤,此函式返回 e 的 z 次冪 (ez),其中 e 表示自然對數的底。

示例 1

以下是一個基本的 C 程式,用於演示如何使用複數的 cexp()

#include <stdio.h>
#include <complex.h>
#include <math.h>
int main()
{
   double complex z = 1 + 2*I;
   double expo = cexp(z);
   printf("z = %.1f + %.1fi\n", creal(z),cimag(z));
   printf("exponent of z = %.1f + %.1fi\n", creal(expo),cimag(expo));
}

輸出

以下是輸出:

z = 1.0 + 2.0i
exponent of z = -1.1 + 0.0i

示例 2

計算 e 的值

讓我們看另一個例子,我們使用尤拉公式,使用 cexp() 函式計算複數的指數。

#include <stdio.h>
#include <math.h>
#include <complex.h>
 
int main(void)
{
   double PI = acos(-1);
   double complex z = cexp(I * PI); // Euler's formula
   printf("exp(i*pi) = %.1f%+.1fi\n", creal(z), cimag(z)); 
}

輸出

以下是輸出:

exp(i*pi) = -1.0+0.0i

示例 3

負複數的指數

在下面的 C 示例中,我們使用 cexp() 函式計算 e-3 + -4i

#include <stdio.h>
#include <complex.h>
#include <math.h>
int main()
{
   double complex z = -3 + -4*I;
   double expo = cexp(z);
   printf("z = %.1f + %.1fi\n", creal(z),cimag(z));
   printf("exponent of z = %.1f + %.1fi\n", creal(expo),cimag(expo));
}

輸出

以下是輸出:

z = -3.0 + -4.0i
exponent of z = -0.0 + 0.0i
c_library_complex_h.htm
廣告
© . All rights reserved.