C library - ctanh() function



The C complex library ctanh() function is used to calculate the hyperbolic tangent of z(complex number). The hyperbolic tangent function has properties similar to the regular tangent function except it is related to hyperbolas instead of circles.

The hyperbolic tangent(tanh) z(complex number) is defined as: tanh(z)= sinh(z)/cosh(z)
This function is depends on the type of z(complex number). If the z is the "float" type, we use ctanhf() to compute tanh, For long double type, use ctanhl(), and for double type, use ctanh().

Syntax

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

double complex ctanh( double complex z );

Parameters

This function accepts a single parameter −

  • Z − It represent a complex number for which we want to calculate tanh.

Return Value

This function returns the complex hyperbolic tangent of z (complex number).

Example 1

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

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

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

   // Calculate the hyperbolic tangent 
   double complex res = ctanh(z);
   printf("Complex tanh: %.2f%+.2fi\n", creal(res), cimag(res));

   return 0;
}

Output

Following is the output −

Complex tanh: 1.00-0.00i

Example 2

Let's see another example, calculate the hyperbolic tangent of real line using ctanh() function.

#include <stdio.h>
#include <math.h>
#include <complex.h>
 
int main(void)
{
   // real line
   double complex z = ctanh(1);
   printf("tanh(1+0i) = %.2f+%.2fi \n", creal(z), cimag(z));
}

Output

Following is the output −

tanh(1+0i) = 0.76+0.00i

Example 3

The below program, calculates both hyperbolic tangent(tanh) and tangent(tan) of the imaginary line of a complex number, and then compares the answer to see if they are same.

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

int main() {
   double complex z = 0.0 + 1.0*I;

   double complex tanh = ctanh(z);
   double complex tan = ctan(z);

   printf("ctanh(%.1fi) = %.2f + %.2fi\n", cimag(z), creal(tanh), cimag(tanh));
   printf("ctan(%.1fi) = %.2f + %.2fi\n", cimag(z), creal(tan), cimag(tan));

   if (cabs(tanh - tan) < 1e-10) {
      printf("The hyperbolic tangent and tangent of the imaginary line are approximately the same.\n");
   } else {
      printf("The hyperbolic tangent and tangent of the imaginary line are different.\n");
   }
   return 0;
}

Output

Following is the output −

ctanh(1.0i) = 0.00 + 1.56i
ctan(1.0i) = 0.00 + 0.76i
The hyperbolic tangent and tangent of the imaginary line are different.
c_library_complex_h.htm
Advertisements