在Go語言中查詢指定數字的正弦值
在數學中,正弦函式是一個三角函式,它表示直角三角形中對角所對邊長度與斜邊長度之比。它用sin表示。在Go語言中,math包提供了Sin()函式,該函式返回給定數字(以弧度為單位)的正弦值。
語法
在Go語言中查詢指定數字正弦值的語法是:
func Sin(x float64) float64
其中x是以弧度表示的角度。
示例1:查詢0.5的正弦值
讓我們編寫一個程式,使用Sin()函式查詢0.5的正弦值。
package main import ( "fmt" "math" ) func main() { x := 0.5 sin := math.Sin(x) fmt.Printf("The sine value of %f is %f\n", x, sin) }
輸出
The sine value of 0.500000 is 0.479426
示例2:查詢π的正弦值
讓我們編寫一個程式,使用Sin()函式查詢π(pi)的正弦值。
package main import ( "fmt" "math" ) func main() { x := math.Pi sin := math.Sin(x) fmt.Printf("The sine value of %f is %f\n", x, sin) }
輸出
The sine value of 3.141593 is 0.000000
示例3:查詢多個數字的正弦值
讓我們編寫一個程式,使用Sin()函式查詢多個數字的正弦值。
package main import ( "fmt" "math" ) func main() { numbers := []float64{0.1, 0.2, 0.3, 0.4, 0.5} for _, x := range numbers { sin := math.Sin(x) fmt.Printf("The sine value of %f is %f\n", x, sin) } }
輸出
The sine value of 0.100000 is 0.099833 The sine value of 0.200000 is 0.198669 The sine value of 0.300000 is 0.295520 The sine value of 0.400000 is 0.389418 The sine value of 0.500000 is 0.479426
結論
Go語言的math包提供的Sin()函式使查詢給定數字(以弧度為單位)的正弦值變得容易。在這篇文章中,我們介紹了Sin()函式的語法,並提供了三個使用它來查詢單個和多個數字的正弦值的示例。
廣告