C library - cpow() function



The C complex library cpow() function is used to calculate the power of the x to the y (i.e. xy), with branch cut for the first parameter along the negative real axis.

This function is depends on the type of z(complex number). If the any argument is of the "float" type, we can use cpowf() to compute complex logarithm, For long double type, use cpowl(), and for double type, use cpow().

Syntax

Following is the C library syntax of cpow() function −

double complex cpow( double complex x, double complex y )

Parameters

This function accepts a following parameter −

  • x − It should be a complex number, a real number, or an imaginary number for we want to calculate the power.

  • y − It represent a number that will be the power of x.

Return Value

This function returns the complex power xy.

Example 1

Following is the basic c program to demonstrate the use of cpow() on a complex number.

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

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

   // Compute the power of the complex number
   double complex result = cpow(z, 2.0);
   printf("Complex number raised to the power of 2: %.2f%+.2fi\n", creal(result), cimag(result));

   return 0;
}

Output

Following is the output −

Complex number raised to the power of 2: -7.00+24.00i

Example 2

Let's see another example, calculate the pow of -ve real number using cpow().

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

int main() {    
   double complex z2 = cpow(-1, 0.5);
   printf("(-1+0i)^0.5 = %.1f%+.1fi\n", creal(z2), cimag(z2));
}

Output

Following is the output −

(-1+0i)^0.5 = 0.0+1.0i

Example 3

The program below, defining a complex number z = -3.0 + - 4.0i and calculating its square using the cpow() function.

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

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

Output

Following is the output −

cpow(-3.00 + -4.00i) = -7.00 + 24.00i
c_library_complex_h.htm
Advertisements