C++ Program to find the hyperbolic cosine of given radian value


Hyperbolic functions, which are defined using the hyperbola rather than the circle, are comparable to normal trigonometric functions. Hyperbolic functions are used in hyperbolic geometry to compute angles and distances. They also show up in the solutions to a large number of linear differential equations, cubic equations, etc. For given angle$\theta$. The hyperbolic cosine function cosh$(\theta)$ is like below

$$\mathrm{cos(x)\:=\:\frac{e^x\:+\:e^{-x}}{2}\:=\:\frac{e^{2x}+1}{2e^x}\:=\:\frac{1+e^{-2x}}{2e^{-x}}}$$

In this article, we shall discuss the techniques to get the value of cosh$(\theta)$ in C++ when the angle is given in the radian unit.

The cosh() function

This cosh$(\theta)$ operation needs the cosh() function from the cmath package in C++. This function returns the result of hyperbolic cosine by taking the angle in radians as input. Here, the simple syntax is used:

Syntax

#include < cmath >
cosh( <angle in radian> )

Algorithm

  • Take angle x in radian as input
  • Use cosh( x ) to calculate the cosh (x)
  • Return result.

Example

#include <iostream> #include <cmath> using namespace std; float solve( float x ) { float answer; answer = cosh( x ); return answer; } int main() { cout << "The value of cosh( pi/2 ) is: " << solve( 3.14159 / 2 ) << endl; cout << "The value of cosh( pi ) is: " << solve( 3.14159 ) << endl; cout << "The value of cosh with an angle of 90 degrees is: " << solve( 90 * 3.14159 / 180 ) << endl; cout << "The value of cosh with an angle of 45 degrees is: " << solve( 45 * 3.14159 / 180 ) << endl; }

Output

The value of cosh( pi/2 ) is: 2.50918
The value of cosh( pi ) is: 11.5919
The value of cosh with an angle of 90 degrees is: 2.50918
The value of cosh with an angle of 45 degrees is: 1.32461

In this example, the first two input values are in radians, whereas the latter two input values are in degrees that have been converted to radians using the following formula:

$$\mathrm{\theta_{rad}\:=\:\theta_{deg}\:\times\:\frac{\pi}{180}}$$

Conclusion

In C++, use the cosh() function to determine the hyperbolic cosine value for the given angle in radians. The cmath header file must be included in our C++ code to utilize this function, even though it is a part of the standard library. If the result is too huge, the cosh() function sets the error code to ERANGE and returns the value HUGE_VAL (which can be either positive or negative depending on the value of x). Later versions of C++ had overloaded methods for float and long double in addition to improved generic (template) usage for integral types, although the C90 version of C++ had a double return type. Various arguments with this function have been used in the article, either in radians or degrees; however, for degrees, the values are converted into radians using the formula given above.

Updated on: 19-Oct-2022

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements