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


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

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

反正弦

定義

反正弦是一個類似於三角函式的函式。反正弦等於值x的反正弦值。反正弦的公式如下所示,反正弦在不同角度的值也不同。

語法

X = sin^-1(Ө)

圖形


不同角度下的反正弦值

  • sin^-1(0) = 0

  • sin^-1(1/2) = ㄫ / 6

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

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

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

輸出

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

演算法

步驟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 ArcSine(angle float64) float64 {
   
   // returning the Arc Sine of the angle
   return math.Asin(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 Sine 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 sine for the given value in a separate function
   answer = ArcSine(value)
   
   // printing the result
   fmt.Println("The Arc Sine value with the value of", value, "is", answer)
}
Program to find the Arc Sine of a given value in the Golang programming language using a separate function in the same program.
The Arc Sine value with the value of 1 is 1.5707963267948966

結論

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

更新於:2022年11月29日

瀏覽量:112

啟動您的職業生涯

完成課程獲得認證

開始
廣告