C庫 - ctan() 函式



C語言的複數ctan() 函式用於計算給定數字的複數正切。複數的正切定義為 tan(z) = sin(z) / cos(z)。此函式通常用於導航領域,例如計算距離、高度和角度。

在C99中,ctan()函式效率不高,因此我們將使用ctanh()函式實現所有程式。

語法

以下是函式的C庫語法:

double complex ctan(double complex z);

引數

它只接受一個引數,即z

返回值

如果沒有錯誤發生,則函式返回z的複數正切。

示例1

以下是C數學庫中ctanh()函式的演示。

#include <stdio.h>
#include <math.h>
double ctanh(double x) {
   return cosh(x) / sinh(x);
}
int main() {
   double input = 2.0; 
   double result = ctanh(input); 
   printf("ctanh(%f) = %f\n", input, result);
   return 0;
}

輸出

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

ctanh(2.000000) = 1.037315

示例2

為了計算雙曲餘切,它使用遞迴方法對給定輸入x計算ctanh值。它接受兩個引數:x(輸入數字)和n(遞迴中的項數)。此外,在函式內部,我們使用以下公式計算級數中的下一項:term = −x * x * ctanh_recursive(x, n−1)。

#include <stdio.h>
double ctanh_recursive(double x, int n) {
   if (n == 0) {
       return 1.0;
   }
   double term = -x * x * ctanh_recursive(x, n - 1);
   return term / (2 * n - 1);
}

int main() {
   // Given input
   double input = 2.0; 
   
   // Number of terms in the recursion
   int terms = 10;     
   double result = ctanh_recursive(input, terms);
   printf("ctanh(%lf) = %lf (approximated with %d terms)\n", input, result, terms);
   return 0;
}

輸出

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

ctanh(2.000000) = 0.001602 (approximated with 10 terms)
c_library_complex_h.htm
廣告
© . All rights reserved.