C++程式查詢給定弧度值的雙曲餘弦


雙曲函式是使用雙曲線而不是圓來定義的,與普通的三角函式類似。雙曲函式用於雙曲幾何中計算角度和距離。它們也出現在大量線性微分方程、三次方程等的解中。對於給定的角度$\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 < cmath >
cosh( <angle in radian> )

演算法

  • 以弧度為單位輸入角度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 << "The value of cosh( pi/2 ) is: " << solve( 3.14159 / 2 ) << endl; cout << "The value of cosh( pi ) is: " << solve( 3.14159 ) << endl; cout << "The value of cosh with an angle of 90 degrees is: " << solve( 90 * 3.14159 / 180 ) << endl; cout << "The value of cosh with an angle of 45 degrees is: " << solve( 45 * 3.14159 / 180 ) << endl; }

輸出

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()函式確定給定弧度角的雙曲餘弦值。即使它是標準庫的一部分,也必須在我們的C++程式碼中包含cmath標頭檔案才能使用此函式。如果結果太大,cosh()函式會將錯誤程式碼設定為ERANGE並返回HUGE_VAL值(根據x的值可以是正數或負數)。雖然C++的C90版本具有雙精度返回型別,但C++的後續版本為浮點數和長雙精度數提供了過載方法,並改進了對整數型別的通用(模板)用法。本文中使用了此函式的各種引數,以弧度或度為單位;但是,對於度數,使用上面給出的公式將其轉換為弧度。

更新於: 2022年10月19日

199 次瀏覽

開啟你的職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.