首頁  >  文章  >  後端開發  >  C++程序,用於計算給定弧度值的雙曲正切值

C++程序,用於計算給定弧度值的雙曲正切值

王林
王林轉載
2023-08-28 13:37:06882瀏覽

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 的方法。文章中使用了該函數的幾個參數,無論是弧度還是度數;但是,對於度數,將使用上面給出的公式將值轉換為弧度。

以上是C++程序,用於計算給定弧度值的雙曲正切值的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除