如何在Go語言中查詢給定弧度值的雙曲正切值?


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

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

雙曲正切

定義

雙曲正切是一個類似於三角函式的函式。代數表示式有助於找到包含指數函式(e^x)的雙曲函式。雙曲正切的公式如下所示,不同角度下雙曲正切的值也列出。

語法

Tanhx = (ex – e-x) / (ex + e-x)

影像


不同角度下雙曲正切的值

  • Tanh(0) = 0

  • Tanh(30) = 1

  • Tanh(45) = 1

  • Tanh(60) = 1

  • Tanh(90) = 1

演算法

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

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

步驟3 - 呼叫雙曲正切函式並傳入弧度值。

步驟4 - 列印結果。

示例

在這個示例中,我們將編寫一個Go程式,在其中我們將匯入一個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 Tangent 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 Tangent for the given radian value
   answer = math.Tanh(radianValue)
   
   // printing the result
   fmt.Println("The hyperbolic Tangent value with the value of radian", radianValue, "is", answer)
}

輸出

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

演算法

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

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

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

步驟4 - 列印結果。

示例

在這個示例中,我們將編寫一個Go程式,在其中我們將匯入一個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 HyperbolicTangent(angle float64) float64 {
   
   // returning the Hyperbolic Tangent of the angle
   return math.Tanh(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 Tangent 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 Tangent for the given radian value in separate function
   answer = HyperbolicTangent(radianValue)
   
   // printing the result
   fmt.Println("The Hyperbolic Tangent value with the value of radian", radianValue, "is", answer)
} 

輸出

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

結論

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

更新於:2022年11月29日

瀏覽量:120

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.