跳轉到內容

C 程式設計/complex.h/carg

來自 Wikibooks,開放世界中的開放書籍
double carg(double complex z);
float cargf(float complex z);
long double cargl(long double complex z);

carg 是一個標準庫函式,用於計算複數的輻角(相位角)。它接受一個引數,並返回該值(如果需要轉換為複數)在 [π, −π] 範圍內的相位角。

此函式接受一個 complex 型別的引數,並返回一個 double 型別的結果。

複數可以表示為

  • 直角座標系中的
  • 極座標系中的

其中

  • A = creal(Z) 是實部
  • B = cimag(Z) 是虛部
  • r = cabs(z) 是半徑
  • a = carg(z) 是相位(角度或輻角)

因此,此函式返回用於極座標的複數 Z 的相位。返回值以弧度表示,範圍為 [π, -π]。當我們使用此函式時,必須包含 <complex.h> 標頭檔案。

[1]

#include <complex.h>
double carg(double complex z);
float cargf(float complex z);
long double cargl(long double complex z);
 /* conversion of a real number from its Cartesian to polar form */

 #include <stdio.h>
 #include <complex.h>
 
 int main() {
   double complex z = -4.4 + 3.3 * I;
   double x = creal(z);
   double y = cimag(z);
 
   double radius = cabs(z);
   double argument = carg(z);
 
   printf("cartesian(x, y): (%4.1f, %4.1f)\n", x, y);
   printf("polar(r, theta): (%4.1f, %4.1f)\n", radius, argument);
   return 0;
 }

輸出:[2]

 Cartesian(x, y): (-4.4, 3.3)
 polar(r, theta): (5.5, 2.5)

參考文獻

[編輯 | 編輯原始碼]
華夏公益教科書