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.