C 庫 - catan() 函式



C 的complexcatan() 函式執行給定複數的反正切(反正切)任務。此函式在標頭檔案 <complex.h> 中定義,自 C99 起可用。

語法

以下是 catan() 函式的 C 庫語法 -

double complex catan(double complex z);

引數

此函式僅接受一個引數 (z),該引數被識別為複數。

返回值

此函式返回 z 的復反正切。

示例 1

以下是演示 catan() 函式用法的 C 庫程式。

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

int main() {
   double complex z = 1.0 + 2.0 * I; 
   double complex result = catan(z);
   printf("catan(%lf + %lfi) = %lf + %lfi\n", creal(z), cimag(z), creal(result), cimag(result));
   return 0;
}

輸出

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

catan(1.000000 + 2.000000i) = 1.338973 + 0.402359i

示例 2

下面的程式建立一個自定義函式 catan_fun(),該函式透過接受兩個引數來執行級數公式的任務 - z(複數)和 n(項數)。這些引數會影響複數的近似值。

#include <stdio.h>
#include <complex.h>

double complex catan_fun(double complex z, int n) {
   double complex sum = z;
   double complex term = z;

   for (int i = 1; i <= n; ++i) {
      term *= -(z * z) / (2 * i + 1);
      sum += term;
   }

   return sum;
}

int main() {
   double complex z = 1.0 + 2.0 * I;
   int terms = 10; 
   double complex result = catan_fun(z, terms);
   printf("catan(%lf + %lfi) = %lf + %lfi (approximated with %d terms)\n", creal(z), cimag(z), creal(result), cimag(result), terms);
   return 0;
}

輸出

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

catan(1.000000 + 2.000000i) = 5.249622 + -2.709977i (approximated with 10 terms)

示例 3

在這裡,我們將再次使用名為遞迴的相同原理。透過使用此過程,使用者可以控制不同精度級別的項引數。我們觀察到引數 z 表示輸入複數,並返回結果作為項值。

#include <stdio.h>
#include <complex.h>

double complex catan_recursive(double complex z, int n) {
   if (n == 0) {
      return z;
   }
   double complex term = -(z * z) / (2 * n + 1) * catan_recursive(z, n - 1);
   return term;
}
 
int main() {
   double complex z = 1.0 + 2.0 * I; 
   
   // input value of no. of terms in recursion
   int terms = 10; 
   double complex result = catan_recursive(z, terms);
   printf("catan(%lf + %lfi) = %lf + %lfi (approximated with %d terms)\n", creal(z), cimag(z), creal(result), cimag(result), terms);
   return 0;
}

輸出

以上程式碼產生以下結果 -

catan(1.000000 + 2.000000i) = -0.000487 + -0.001512i (approximated with 10 terms)
c_library_complex_h.htm
廣告

© . All rights reserved.