如何在 Golang 中查詢給定弧度值的餘弦?


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

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

餘弦

定義

餘弦是三角函式的一部分。要理解餘弦,請觀察下圖。


如果我們藉助上圖定義餘弦。則角度 Ө 與餘弦函式相等,等於鄰邊與斜邊的比值。

cosӨ = 鄰邊 / 斜邊

不同角度的餘弦值

  • cos(0) = 1

  • cos(30) = √3 / 2

  • cos(45) = 1 / √2

  • cos(60) = 1 / 2

  • cos(90) = 0

  • cos(120) = -1 / 2

  • cos(135) = - 1 / √2

  • cos(150) = -√3 / 2

  • cos(180) = - 1

圖形

現在我們將檢視餘弦函式的圖形,並在圖形上觀察上述值。對於零角度,值為 1,然後直到角度變為 90 度,值為零。然後再次直到 180 度,我們將在第四象限得到一個映象。


演算法

步驟 1 - 宣告變數以儲存 guardian 和 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 cosine of a given radian value in the Golang programming language using a math package.")
   
   // initializing the value of radian value
   radianValue = 4.5
   
   // finding cosine for the given radian value
   answer = math.Cos(radianValue)
   
   // printing the result
   fmt.Println("The cosine value with the value of radian", radianValue, "is", answer)
} 

輸出

Program to find the cosine of a given radian value in the Golang programming language using a math package.
The cosine value with the value of radian 4.5 is -0.21079579943077972

演算法

步驟 1 - 宣告變數以儲存 guardian 和 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 Cosine(angle float64) float64 {
   
   // returning the cosine of the angle
   return math.Cos(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 cosine 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 cosine for the given radian value in separate function
   answer = Cosine(radianValue)
   
   // printing the result
   fmt.Println("The cosine value with the value of radian", radianValue, "is", answer)
} 

輸出

Program to find the cosine of a given radian value in the Golang programming language using a separate function in the same program.
The cosine value with the value of radian 4.5 is -0.21079579943077972

結論

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

更新於: 2022-11-29

204 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.