卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16517本站已运行3318

C++程序,用于计算给定弧度值的双曲正切值

C++程序,用于计算给定弧度值的双曲正切值

与常规三角函数类似,双曲函数是使用双曲线而不是圆来定义的。在双曲几何中,双曲函数用于计算角度和距离。此外,它们还可以在大量线性微分方程、三次方程等的答案中找到。对于给定的角度$\theta$。双曲正切函数 tanh$(\theta)$ 如下 -

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

在本文中,我们将讨论当角度以弧度单位给出时,在 C++ 中获取 tanh$(\theta)$ 值的技术。

tanh() 函数

此 tanh$(\theta)$ 需要 C++ cmath 库中的 tanh() 函数才能运行。该函数将弧度角度作为输入并输出双曲余弦值。下面使用简单的语法。

语法

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

算法

  • 将角度 x(以弧度表示)作为输入。
  • 使用 tanh( x ) 计算 tanh (x)。
  • 返回结果。

示例

#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;
}

输出

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

此示例中的前两个输入数字以弧度为单位,而后两个是已使用以下公式转换为弧度的度数 -

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

结论

要在 C++ 中计算给定角度(以弧度为单位)的双曲正切值,请使用 tanh() 函数。尽管 cmath 头文件是标准库的一部分,但需要将其包含在我们的 C++ 代码中才能使用此函数。 tanh() 函数返回值 HUGE VAL,如果结果太大(可以是正数或负数,具体取决于 x 的值),则将错误代码设置为 ERANGE。尽管 C++ 的 C90 版本具有双返回类型,但更高版本的 C++ 除了对整型类型有更好的泛型(模板)用法之外,还重载了 float 和 long double 的方法。文章中使用了该函数的几个参数,无论是弧度还是度数;但是,对于度数,将使用上面给出的公式将值转换为弧度。

卓越飞翔博客
上一篇: 进行字母频率攻击的单字母替代密码程序
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏