Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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
<div class="execute"></div><div class="code-mirror language-cpp" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><iostream></span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string"><cmath></span></span>
<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>
<span class="token keyword">float</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token keyword">float</span> x <span class="token punctuation">)</span> <span class="token punctuation">{</span>
<span class="token keyword">float</span> answer<span class="token punctuation">;</span>
answer <span class="token operator">=</span> <span class="token function">tanh</span><span class="token punctuation">(</span> x <span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token keyword">return</span> answer<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
<span class="token punctuation">{</span>
cout <span class="token operator"><<</span> <span class="token string">"The value of tanh( pi/2 ) is: "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">3.14159</span> <span class="token operator">/</span> <span class="token number">2</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
cout <span class="token operator"><<</span> <span class="token string">"The value of tanh( pi ) is: "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">3.14159</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
cout <span class="token operator"><<</span> <span class="token string">"The value of tanh with an angle of 90 degrees is: "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">90</span> <span class="token operator">*</span> <span class="token number">3.14159</span> <span class="token operator">/</span> <span class="token number">180</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
cout <span class="token operator"><<</span> <span class="token string">"The value of tanh with an angle of 45 degrees is: "</span> <span class="token operator"><<</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">45</span> <span class="token operator">*</span> <span class="token number">3.14159</span> <span class="token operator">/</span> <span class="token number">180</span> <span class="token punctuation">)</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
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.
