C 庫 - csqrt() 函式



C 的complexcsqrt() 函式用於計算 z(複數)的平方根,其分支切割沿負實軸。

此函式取決於 z(複數)的型別。如果 z 是“float”型別,我們可以使用csqrtf()計算複數平方根,對於長雙精度型別,使用csqrtl(),對於雙精度型別,使用csqrt()

語法

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

double complex csqrt( double complex z );

引數

此函式接受一個引數 -

  • Z - 它表示我們要計算平方根的複數。

返回值

此函式返回 z 的平方根。它應該在右半平面的範圍內,包括虛軸:[0, +∞) 沿實軸和(-∞, +∞) 沿虛軸。

示例 1

以下是演示如何在複數上使用csqrt() 的基本 C 程式。

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

int main() {
   double complex z = 3.0 + 4.0 * I;

   // Compute the sqaure root of z
   double complex result = csqrt(z);
   printf("Complex square root of z: %.2f%+.2fi\n", creal(result), cimag(result));

   return 0;
}

輸出

以下是輸出 -

Complex square root of z: 2.00+1.00i

示例 2

讓我們看另一個例子,使用csqrt() 函式計算負實數的平方根。

#include <stdio.h>
#include <complex.h> 
int main(void)
{
   double complex z1 = csqrt(-4);
   printf("Square root of -4 is %.1f%+.1fi\n", creal(z1), cimag(z1));    
}

輸出

以下是輸出 -

Square root of -4 is 0.0+2.0i

示例 3

下面的程式定義了一個複數 z = 4.0i,並使用csqrt() 函式計算其平方。

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

int main() {
   double complex z = 4.0*I; 
   //use csqrt()
   double complex res = csqrt(z);
   printf("csqrt(%.2f + %.2fi) = %.2f + %.2fi\n", creal(z), cimag(z), creal(res), cimag(res));
   return 0;
}

輸出

以下是輸出 -

csqrt(0.00 + 4.00i) = 1.41 + 1.41i
c_library_complex_h.htm
廣告

© . All rights reserved.