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


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

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

雙曲正弦

定義

雙曲正弦是一個類似於三角函式的函式。代數表示式有助於找到包含指數函式 (e^x) 的雙曲函式。下面寫出了雙曲正弦的公式,以及雙曲正弦在不同角度下的值。

語法

Sinhx = (e^x - e^-x) / 2 

圖形


雙曲正弦在不同角度下的值

  • sinh(0) = 0

  • sinh(30) = 5.3432373e+12

  • sinh(45) = 1.7467136e+19

  • sinh(60) = 5.7100369e+25

  • sinh(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 sine 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 sine for the given radian value
   answer = math.Sinh(radianValue)
   
   // printing the result
   fmt.Println("The hyperbolic sine value with the value of radian", radianValue, "is", answer)
} 

輸出

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

演算法

步驟 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 HyperbolicSine(angle float64) float64 {
   
   // returning the Hyperbolic Sine of the angle
   return math.Sinh(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 Sine 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 hypsine for the given radian value in separate function
   answer = HyperbolicSine(radianValue)
   
   // printing the result
   fmt.Println("The Hyperbolic Sine value with the value of radian", radianValue, "is", answer)
} 

輸出

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

結論

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

更新於: 2022-11-29

116 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.