
双曲函数是使用双曲线而不是圆定义的,与普通三角函数相当。双曲函数在双曲几何中用于计算角度和距离。它们还出现在大量线性微分方程、三次方程等的解中。对于给定的角度$\theta$。双曲余弦函数 cosh$(\theta)$ 如下
$$\mathrm{cos(x)\:=\:\frac{e^x\:+\:e^{-x}}{2}\:=\:\frac{e^{2x }+1}{2e^x}\:=\:\frac{1+e^{-2x}}{2e^{-x}}}$$
在本文中,我们将讨论当角度以弧度单位给出时,在 C++ 中获取 cosh$(\theta)$ 值的技术。
cosh() 函数
这个 cosh$(\theta)$ 操作需要 C++ 中 cmath 包中的 cosh() 函数。该函数以弧度角度作为输入,返回双曲余弦结果。这里使用简单的语法:
语法
#include cosh( <angle in radian> ) </angle>
算法
- 将角度 x(以弧度表示)作为输入
- 使用 cosh( x ) 计算 cosh (x)
- 返回结果。
示例
#include <iostream>
#include <cmath>
using namespace std;
float solve( float x ) {
float answer;
answer = cosh( x );
return answer;
}
int main()
{
cout <h3>输出</h3>
<pre class="brush:php;toolbar:false;">The value of cosh( pi/2 ) is: 2.50918
The value of cosh( pi ) is: 11.5919
The value of cosh with an angle of 90 degrees is: 2.50918
The value of cosh with an angle of 45 degrees is: 1.32461
在此示例中,前两个输入值以弧度为单位,而后两个输入值以度为单位,已使用以下公式转换为弧度:
$$\mathrm{\theta_{rad}\:=\:\theta_{deg}\:\times\:\frac{\pi}{180}}$$
结论
在 C++ 中,使用 cosh() 函数确定给定角度(以弧度为单位)的双曲余弦值。 cmath 头文件必须包含在我们的 C++ 代码中才能使用此函数,即使它是标准库的一部分。如果结果太大,则 cosh() 函数将错误代码设置为 ERANGE 并返回值 HUGE_VAL(可以是正值或负值,具体取决于 x 的值)。尽管 C90 版本的 C++ 具有双精度返回类型,但后来的 C++ 版本除了改进了整型的泛型(模板)用法之外,还重载了 float 和 long double 的方法。文章中使用了该函数的各种参数,以弧度或度为单位;但是,对于度数,将使用上面给出的公式将值转换为弧度。
C++免费学习笔记(深入):立即使用
在学习笔记中,你将探索 C++ 的入门与实战技巧!










