如何在Go語言中找到給定值的雙曲反正切?
在本教程中,我們將學習如何在Go程式語言中找到給定值的雙曲反正切。Go語言擁有許多包含預定義函式的包,開發者可以使用這些函式而無需編寫完整的邏輯。
為了執行數學運算和邏輯,Go語言中有一個math包。我們將僅使用此包來查詢給定值的雙曲反正切。我們還將學習如何匯入包,以及如何透過編寫Go程式碼來呼叫該包包含的函式。
雙曲反正切
定義
雙曲反正切是一個類似於三角函式的函式。雙曲反正切等於三角函式的類似物。雙曲反正切的公式如下所示,雙曲反正切在不同角度的值也不同。
語法
atanh(x) = 1/2 * ln(1+x/1-x)
圖形
雙曲反正切在不同角度的值
atanh(0) = 0
atanh(30) = NaN
atanh(45) = NaN
atanh(60) = NaN
atanh(90) = NaN
演算法
步驟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 \value and answer var value, answer float64 fmt.Println("Program to find the Hyperbolic Arc Tangent of a given value in the Golang programming language using a math package.") // initializing the value of the variable value value = 0.5 // finding Hyperbolic Arc Tangent for the given value answer = math.Atanh(value) // printing the result fmt.Println("The Hyperbolic Arc Tangent value with the value of", value, "is", answer) }
輸出
Program to find the Hyperbolic Arc Tangent of a given value in the Golang programming language using a math package. The Hyperbolic Arc Tangent value with the value of 0.5 is 0.5493061443340548
演算法
步驟1 - 宣告變數以儲存float32型別的守護值和答案。
步驟2 - 初始化變數的值。
步驟3 - 呼叫我們定義的雙曲反正切函式並將值作為引數傳遞。
步驟4 - 列印結果。
示例
在這個例子中,我們將編寫一個Go程式,在這個程式中我們將匯入math包,在一個單獨的函式中呼叫雙曲反正切函式,然後在主函式中呼叫該函式。
package main import ( "fmt" "math" ) type of float64 func HyperbolicArcTangent(angle float64) float64 { // returning the Hyperbolic Arc Tangent of the angle return math.Atanh(angle) } func main() { // declaring the variables to store the value of value and answer var value, answer float64 fmt.Println("Program to find the Hyperbolic Arc Tangent of a given value in the Golang programming language using a separate function in the same program.") // initializing the value of the value value = 0.5 // finding Hyperbolic Arc Tangent for the given value in a separate function answer = HyperbolicArcTangent(value) // printing the result fmt.Println("The Hyperbolic Arc Tangent value with the value of", value, "is", answer) }
輸出
Program to find the Hyperbolic Arc Tangent of a given value in the Golang programming language using a separate function in the same program. The Hyperbolic Arc Tangent value with the value of 0.5 is 0.5493061443340548
結論
這是兩種使用math包中的函式並將值作為引數傳遞來查詢雙曲反正切的方法。第二種方法將為程式提供抽象。要了解更多關於Go的資訊,您可以瀏覽這些教程。
廣告