C library - catan() function



The C complex library catan() function operate the task of inverse tangent(arctangent) of a given complex number. This function defined under the header <complex.h> and it is available since C99.

Syntax

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

double complex catan(double complex z);

Parameters

This function takes only a single parameter(z) which is identified as complex number.

Return Value

This function returns complex arctangent of z.

Example 1

Following is the C library program that demonstrates the usage of catan() function.

#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;
}

Output

On execution of above code, we get the following result −

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

Example 2

Below the program create a custom function catan_fun() which perform the task of series formula by accepting two parameters − z(complex number) and n(number of terms). These parameter affect the approximate value of a complex number.

#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;
}

Output

After executing the above code, we get the following result −

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

Example 3

Here, we will again use the same principle named recursion. By using this process user can control the term parameter for different levels of accuracy. we observe that the parameter z represents the input complex number and returns the result as terms value.

#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;
}

Output

The above code produces the following result −

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