如何在 Golang 中查詢給定弧度值的雙曲餘弦?


在本教程中,我們將學習如何在 Golang 程式語言中查詢給定弧度值的雙曲餘弦。Golang 語言擁有許多包含預定義函式的包,開發人員可以使用這些函式而無需編寫完整的邏輯。

為了執行數學運算和邏輯,我們在 Golang 中有一個 **math** 包。我們只使用此包來查詢給定弧度值的雙曲餘弦。我們還將瞭解如何匯入包以及如何透過編寫 Golang 程式碼來呼叫此包包含的函式。

雙曲餘弦

定義

雙曲餘弦是一個類似於三角函式的函式。代數表示式有助於找到包括指數函式 (e^x) 的雙曲函式。雙曲餘弦的公式如下所示,雙曲餘弦在不同角度的值如下。

語法

Coshx = (e^x + e^-x) / 2 

圖形


雙曲餘弦在不同角度的值

  • cosh(0) = 1

  • cosh(30) = 5.3432373e+12

  • cosh(45) = 1.7467136e+19

  • cosh(60) = 5.7100369e+25

  • cosh(90) = 6.1020165e+38

演算法

**步驟 1** - 宣告變數以儲存 float32 型別的弧度值和結果。

**步驟 2** - 初始化弧度變數。

**步驟 3** - 呼叫雙曲餘弦函式並傳遞弧度值。

**步驟 4** - 列印結果。

示例

在此示例中,我們將編寫一個 Golang 程式,在其中我們將匯入 **math** 包並呼叫雙曲餘弦函式。

package main
import (
   
   // fmt package provides the function to print anything
   "fmt"
   
   // math package provides multiple functions for different
   
   // mathematical operations
   "math"
)
func main() {
   
   // declaring the variables to store the value of radian value and answer
   var radianValue, answer float64
   fmt.Println("Program to find the hyperbolic Cosine of a given radian value in the Golang programming language using a math package.")
   
   // initializing the value of radian value
   radianValue = 4.5
   
   // finding Hyperbolic Cosine for the given radian value
   answer = math.Cosh(radianValue)
   
   // printing the result
   fmt.Println("The hyperbolic Cosine value with the value of radian", radianValue, "is", answer)
}

輸出

Program to find the hyperbolic Cosine of a given radian value in the Golang programming language using a math package.
The hyperbolic Cosine value with the value of radian 4.5 is 45.014120148530026

演算法

步驟 1 - 宣告變數以儲存 float32 型別的弧度值和結果。

步驟 2 - 初始化弧度變數。

步驟 3 - 呼叫我們定義的雙曲餘弦函式並將弧度值作為引數傳遞。

步驟 4 - 列印結果。

示例

在此示例中,我們將編寫一個 Golang 程式,在其中我們將匯入 **math** 包並在單獨的函式中呼叫雙曲餘弦函式,並在主函式中呼叫該函式。

package main
import (
   
   // fmt package provides the function to print anything
   "fmt"
   
   // math package provides multiple functions for different
   
   // mathematical operations
   "math"
)

// this is a function with a parameter of float64 type and a return type of float64
func HyperbolicCosine(angle float64) float64 {
   
   // returning the Hyperbolic Cosine of the angle
   return math.Cosh(angle)
}
func main() {
   
   // declaring the variables to store the value of radian value and answer
   var radianValue, answer float64
   fmt.Println("Program to find the Hyperbolic Cosine of a given radian value in the Golang programming language using a separate function in the same program.")
   
   // initializing the value of the radian value
   radianValue = 4.5
   
   // finding hyperbolic Cosine for the given radian value in separate function
   answer = HyperbolicCosine(radianValue)
   
   // printing the result
   fmt.Println("The Hyperbolic Cosine value with the value of radian", radianValue, "is", answer)
}

輸出

Program to find the Hyperbolic Cosine of a given radian value in the Golang programming language using a separate function in the same program.
The Hyperbolic Cosine value with the value of radian 4.5 is 45.014120148530026

結論

這兩種方法都是使用 math 包中的函式並將弧度值作為引數傳遞來查詢雙曲餘弦。第二種方法將為程式提供抽象。要了解有關 Golang 的更多資訊,您可以瀏覽這些 教程。

更新於:2022-11-29

114 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

立即開始
廣告

© . All rights reserved.