C library - ccosh() function



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

The hyperbolic cos(cosh) z(complex number) is defined as:
cosh(z)= ez + e-z/2

This function is depends on the type of z(complex number). If the z is the "float" type, we use ccoshf() to compute cosh, For long double type, use ccoshl(), and for double type, use ccosh().

Syntax

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

double complex ccosh( double complex z );

Parameters

This function accepts a single parameter −

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

Return Value

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

Example 1

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

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

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

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

   return 0;
}

Output

Following is the output −

Complex cosh: -3.72+0.51i

Example 2

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

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

Output

Following is the output −

cosh(1+0i) = 1.543081+0.000000i (cosh(1)=1.543081)

Example 3

The below program, calculates the hyperbolic cosine of the imaginary line using ccosh() function.

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

Output

Following is the output −

cosh(0+1i) = 0.540302+0.000000i ( cos(1)=0.841471)
c_library_complex_h.htm
Advertisements