如何在 Golang 中找到給定值的 Arc Cosine(反餘弦)?


在本教程中,我們將學習如何在 Golang 程式語言中找到給定值的 Arc Cosine(反餘弦)。Golang 語言包含許多帶有預定義函式的包,開發人員可以使用這些函式而無需編寫完整的邏輯。

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

Arc Cosine(反餘弦)

定義

Arc Cosine(反餘弦)是一個類似於三角函式的函式。Arc Cosine(反餘弦)等於值 x 的反函式。Arc Cosine(反餘弦)的公式如下所示,以及 Arc Cosine(反餘弦)在不同角度的值。

語法

X = cos^-1(Ө)

圖形


Arc Cosine(反餘弦)在不同角度的值

  • cos^-1(1) = 0

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

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

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

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

演算法

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

步驟 2 - 初始化變數的值。

步驟 3 - 呼叫 Arc Cosine 函式並傳遞值。

步驟 4 - 列印結果。

示例

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

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

輸出

Program to find the Arc Cosine of a given value in the Golang programming language using a math package.
The Arc Cosine value with the value of 0 is 1.5707963267948966

演算法

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

步驟 2 - 初始化變數的值。

步驟 3 - 呼叫我們定義的 Arc Cosine 函式並將值作為引數傳遞。

步驟 4 - 列印結果。

示例

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

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 ArcCosine(angle float64) float64 {
   
   // returning the Arc Cosine of the angle
   return math.Acos(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 Cosine 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
   
   // finding Arc Cosine for the given value in a separate function
   answer = ArcCosine(value)
   
   // printing the result
   fmt.Println("The Arc Cosine value with the value of", value, "is", answer)
}

輸出

Program to find the Arc Cosine of a given value in the Golang programming language using a separate function in the same program.
The Arc Cosine value with the value of 0 is 1.5707963267948966

結論

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

更新於:2022-11-29

123 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告