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()函数。尽管它是标准库的一部分,但为了使用此函数,需要在我们的C++代码中包含cmath头文件。如果结果过大(根据x的值可能是正数或负数),tanh()函数将返回HUGE_VAL值并将错误代码设置为ERANGE。虽然C++的C90版本具有双精度返回类型,但后来的C++版本除了更好地支持通用(模板)整型用法外,还为float和long double重载了方法。本文使用了多个此函数的参数,无论是弧度还是度数;但是,对于度数,其值使用上面给出的公式转换为弧度。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP