Home >Backend Development >C++ >C++ program to calculate the hyperbolic tangent of a given value in radians

C++ program to calculate the hyperbolic tangent of a given value in radians

王林
王林forward
2023-08-28 13:37:06963browse

C++ program to calculate the hyperbolic tangent of a given value in radians

Like regular trigonometric functions, hyperbolic functions are defined using hyperbolas instead of circles. In hyperbolic geometry, hyperbolic functions are used to calculate angles and distances. Additionally, they can be found in the answers to a large number of linear differential equations, cubic equations, etc. For a given angle $\theta$. The hyperbolic tangent function tanh$(\theta)$ is as follows -

$$\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 will discuss techniques for obtaining the value of tanh$(\theta)$ in C when the angle is given in radians.

tanh() function

This tanh$(\theta)$ requires the tanh() function in the C cmath library to run. This function takes an angle in radians as input and outputs a hyperbolic cosine value. Simple syntax is used below.

grammar

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

algorithm

  • Take angle x (expressed in radians) as input.
  • Use tanh(x) to calculate tanh (x).
  • Return results.

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, while the last two are degrees that have been converted to radians using the following formula -

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

in conclusion

To calculate the hyperbolic tangent of a given angle in radians in C, use the tanh() function. Although the cmath header file is part of the standard library, it needs to be included in our C code 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 (can be positive or negative, depending on the value of x). Although the C90 version of C had a double return type, later versions of C overloaded methods for float and long double in addition to better generic (template) usage for integral types. Several parameters of this function are used in the article, whether in radians or degrees; however, for degrees, the value is converted to radians using the formula given above.

The above is the detailed content of C++ program to calculate the hyperbolic tangent of a given value in radians. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete