cosh() function for complex number working with C++

C++Server Side ProgrammingProgramming

Given the task is to show the working of cosh() function for complex numbers in C++.

The cosh() function is a part of the C++ standard template library. It is a little different from the standard cosh() function. Instead of calculating the hyperbolic cosines of angles that are in radians, it calculates the complex hyperbolic cosine values of complex numbers.

The mathematical formula for calculating complex hyperbolic cosine is

cosh(z) = (e^(z) + e^(-z))/z

Where, “z” represents the complex number and “i” represents the iota.

The complex number should be declared as follows

complex<double> name(a,b)

Here, <double> that is attached to the “complex” data type describes an object that stores ordered pair of objects, both of type “double’. Here the two objects are the real part and the imaginary part of the complex number that we want to enter. <complex> header file should be included to call the function for complex numbers.

Syntax

The syntax is as follows −

cosh(complexnumber)

Example

Input: complexnumber(5,5)
Output: <-27.0349,-3.85115>

Explanation − The following example shows how we use the cosh() function for calculating the complex hyperbolic cosine values of a complex number. Here 5 is the real part and another 5 is the imaginary part of the complex number as shown in the input, and the we get the hyperbolic cosine values in the output as we pass the complex number into the cosh() function.

Approach used in the below program as follows 

  • First declare a complex number, let’s say complexnumber(a,b)and then assign it a complex value.
  • Two values should be assigned to the variable complexnumber(a,b). The first value will be the real part of the complex number and the second value will be the imaginary part of the complex number.

    Let us say complexnumber(1, 3) so this will represent the complex number 1+3i.

  • Now pass the complexnumber(1, 3)we created into the cosh() function

Example

 Live Demo

#include<iostream>
#include<complex>
using namespace std;
int main() {
   complex<double> cno(1,3);
   cout<<cosh(cno);
   return 0;
}

Output

If we run the above code it will generate the following output −

<-1.52764,0.165844>

Here 1 is the real part and 3 is the imaginary part of the complex number, as we pass our complex number into the cosh() function, we get the hyperbolic cosine values in the output as shown.

raja
Published on 20-Jan-2020 07:32:37
Advertisements