如何在Go語言中查詢給定值的反正切?


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

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

反正切

定義

反正切是一個類似於三角函式的函式。反正切等於值x的倒數。反正切的公式如下所示,並且反正切的值在不同的角度上是不同的。

語法

X = tan^-1(Ө)

圖形


不同角度下反正切的值

  • tan^-1(1) = 0

  • tan^-1(√3/2) = ㄫ / 6

  • tan^-1(1/√2) = ㄫ / 4

  • tan^-1(1/2) = ㄫ / 3

  • tan^-1(0) = ㄫ / 2

演算法

步驟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 Arc Tangent of a given value in the Golang programming language using a math package.")
   
   // initializing the value of the variable value
   value = 1
   
   // finding Arc Tangent for the given value 
   answer = math.Atan(value)
   
   // printing the result
   fmt.Println("The Arc Tangent value with the value of", value, "is", answer)
} 

輸出

Program to find the Arc Tangent of a given value in the Golang programming language using a math package.
The Arc Tangent value with the value of 1 is 0.7853981633974483

演算法

步驟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"
)
type of float64
func ArcTangent(angle float64) float64 {
   
   // returning the Arc Tangent of the angle
   return math.Atan(angle)
} 
func main() {
   
   // declaring the variables to store the value of value and answer
   var value, answer float64
   fmt.Println("Program to find the 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 = 1
   
   // finding Arc Tangent for the given value in a separate function
   answer = ArcTangent(value)
   
   // printing the result
   fmt.Println("The Arc Tangent value with the value of", value, "is", answer)
} 
Program to find the Arc Tangent of a given value in the Golang programming language using a separate function in the same program.
The Arc Tangent value with the value of 1 is 0.7853981633974483

結論

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

更新於:2022年11月29日

133 次瀏覽

啟動您的職業生涯

完成課程獲得認證

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