C++ Program to find the hyperbolic arctangent of the given value

The hyperbola, rather than the circle, is used to define hyperbolic functions. It returns the ratio parameter for the hyperbolic tangent function based on the supplied radian angle. On the contrary, however. To calculate the angle from the hyperbolic-tangent value, inverse hyperbolic trigonometric procedures like the hyperbolic arctangent operation are required.

This article will demonstrate how to utilize the C++ hyperbolic inverse-tangent (atanh) function to determine the angle using the hyperbolic tangent value, in radians. The hyperbolic inverse-tangent operation has the following formula ?

$$\mathrm{cosh^{-1}x\:=\:\frac{1}{2}In\left(\frac{1\:+\:x}{1\:-\:x}\right)},where \:In\: is\: natural\: logarithm\:(log_e \: k)$$

The atanh() function

The angle can be calculated from the hyperbolic tangent value using the atanh() function. This function is a part of the C++ standard library. It is necessary to import the cmath library before using this function. When a hyperbolic tangent value is provided, this procedure provides the angle in radians. The following uses the simple syntax ?

Syntax

#include  ? cmath >
atanh( ?hyperbolic tangent value> )

The input range for this function is [-1 to 1] (both included). If the input is out of this range, it will raise a domain error.

Algorithm

  • Take hyperbolic tangent value x as input
  • Use atanh( x ) to calculate the tanh?1(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">atanh</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>
   <span class="token keyword">float</span> angle<span class="token punctuation">,</span> ang_deg<span class="token punctuation">;</span>
   angle <span class="token operator">=</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">0.9171521</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
   ang_deg <span class="token operator">=</span> angle <span class="token operator">*</span> <span class="token number">180</span> <span class="token operator">/</span> <span class="token number">3.14159</span><span class="token punctuation">;</span>

   cout <span class="token operator"><<</span> <span class="token string">"The angle (in radian) for given hyperbolic tangent value 0.9171521 is: "</span> <span class="token operator"><<</span> angle <span class="token operator"><<</span> <span class="token string">" = "</span> <span class="token operator"><<</span> ang_deg <span class="token operator"><<</span> <span class="token string">" (in degrees)"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>

   angle <span class="token operator">=</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">0.996272</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
   ang_deg <span class="token operator">=</span> angle <span class="token operator">*</span> <span class="token number">180</span> <span class="token operator">/</span> <span class="token number">3.14159</span><span class="token punctuation">;</span>

   cout <span class="token operator"><<</span> <span class="token string">"The angle (in radian) for given hyperbolic tangent value 0.996272 is: "</span> <span class="token operator"><<</span> angle <span class="token operator"><<</span> <span class="token string">" = "</span> <span class="token operator"><<</span> ang_deg <span class="token operator"><<</span> <span class="token string">" (in degrees)"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>

   angle <span class="token operator">=</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token number">0.655794</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
   ang_deg <span class="token operator">=</span> angle <span class="token operator">*</span> <span class="token number">180</span> <span class="token operator">/</span> <span class="token number">3.14159</span><span class="token punctuation">;</span>

   cout <span class="token operator"><<</span> <span class="token string">"The angle (in radian) for given hyperbolic tangent value 0.655794 is: "</span> <span class="token operator"><<</span> angle <span class="token operator"><<</span> <span class="token string">" = "</span> <span class="token operator"><<</span> ang_deg <span class="token operator"><<</span> <span class="token string">" (in degrees)"</span> <span class="token operator"><<</span> endl<span class="token punctuation">;</span>

   angle <span class="token operator">=</span> <span class="token function">solve</span><span class="token punctuation">(</span> <span class="token operator">-</span><span class="token number">0.655794</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
   ang_deg <span class="token operator">=</span> angle <span class="token operator">*</span> <span class="token number">180</span> <span class="token operator">/</span> <span class="token number">3.14159</span><span class="token punctuation">;</span>

   cout <span class="token operator"><<</span> <span class="token string">"The angle (in radian) for given hyperbolic tangent value - 0.655794 is: "</span> <span class="token operator"><<</span> angle <span class="token operator"><<</span> <span class="token string">" = "</span> <span class="token operator"><<</span> ang_deg <span class="token operator"><<</span> <span class="token string">" (in degrees)"</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 angle (in radian) for given hyperbolic tangent value 0.9171521 is: 1.57079 = 90 (in degrees)
The angle (in radian) for given hyperbolic tangent value 0.996272 is: 3.14159 = 180 (in degrees)
The angle (in radian) for given hyperbolic tangent value 0.655794 is: 0.785398 = 45 (in degrees)The angle (in radian) for given hyperbolic tangent value - 0.655794 is: -0.785398 = -45 (in degrees)

The atanh() method receives the value of the hyperbolic tangent and returns the angle in radian format. We converted this output from radians to degrees using the formula below.

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

Conclusion

We use the hyperbolic tangent value to conduct the inverse hyperbolic operation using the atanh() function from the cmath library. Based on the input value of the hyperbolic tangent, this function returns the desired angle in radians. The input's range is from -1 to +1. When the input value is outside of the range, the domain error is raised. In early iterations of C and C++, the return type was double; in later iterations of C++, the overloaded form for float and long-double was also used. The atanh() method will be used after casting the input parameter into the double type when an integer value is provided as an argument.

Updated on: 2022-10-19T08:49:04+05:30

267 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements