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


Similar to regular trigonometric functions, hyperbolic functions are defined using the hyperbola rather than the circle. In hyperbolic geometry, hyperbolic functions are used to calculate angles and distances. In addition, they are found in the answers to plenty of linear differential equations, cubic equations, etc. For given angle$\theta$. The hyperbolic tangent function tanh$(\theta)$ is like below −

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

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

The tanh() function

This tanh$(\theta)$ The tanh() function from the C++ cmath library is required for operation. This function takes an angle in radians as an input and outputs the hyperbolic cosine value. The following uses the simple syntax.

Syntax

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

Algorithm

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

Example

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

Output

The value of tanh( pi/2 ) is: 0.917152
The value of tanh( pi ) is: 0.996272
The value of tanh with an angle of 90 degrees is: 0.917152
The value of tanh with an angle of 45 degrees is: 0.655794

The first two input numbers in this example are in radians, whereas the latter two are degrees that have been converted to radians using the formula below −

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

Conclusion

To calculate the hyperbolic tangent value for a given angle in radians in C++, use the tanh() function. Despite being a part of the standard library, the cmath header file needs to be included in our C++ code in order to use this function. The tanh() function returns the value HUGE VAL and sets the error code to ERANGE if the result is too large (which can be either positive or negative depending on the value of x). Although the C90 version of C++ featured a double return type, later versions of C++ have overloaded methods for float and long double in addition to better generic (template) usage for integral types. Several arguments with this function, either in radians or degrees, have been used in the article; however, for degrees, the values are converted into radians using the formula given above.

Updated on: 19-Oct-2022

160 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements