Golang 查找指定数字的正弦值
Go语言在math包中提供了内建的基本常数和数学函数,以便对数值进行操作。你可以使用math包中提供的Sin()函数来找到指定弧度参数的正弦值。因此,你需要使用import关键字添加一个math包到你的程序中,以便访问Sin()函数。
Syntax:
func Sin(y float64) float64
- 如果你在该函数中传递+Inf或-Inf,则函数返回NaN(不是数字)。
- 如果你在该函数中传递NaN,则函数返回NaN。
- 如果你在该函数中传递0,则函数返回0。
示例1:
//Golang示例程序,用于说明如何找到给定弧度参数的正弦值
package main
import (
"fmt"
"math"
)
//主函数
func main() {
//查找给定值的正弦值
//使用Sin()函数
res_1 := math.Sin(math.Pi / 4)
res_2 := math.Sin(math.Inf(-3))
res_3 := math.Sin(0)
res_4 := math.Sin(math.NaN())
res_5 := math.Sin(math.Pi)
//显示结果
fmt.Printf("Result 1: %.1f", res_1)
fmt.Printf("\nResult 2: %.1f", res_2)
fmt.Printf("\nResult 3: %.1f", res_3)
fmt.Printf("\nResult 4: %.1f", res_4)
fmt.Printf("\nResult 5: %.1f", res_5)
}
输出:
Result 1: 0.7
Result 2: NaN
Result 3: 0.0
Result 4: NaN
Result 5: 0.0
示例2:
//Golang示例程序,用于说明如何找到给定弧度参数的正弦值
package main
import (
"fmt"
"math"
)
//主函数
func main() {
//查找给定数字的正弦值
nvalue_1 := math.Sin(math.Pi / 3)
nvalue_2 := math.Sin(math.Pi / 2)
//两个给定值的和
res := nvalue_1 + nvalue_2
//显示结果
fmt.Printf("%.1f + %.1f = %.1f",
nvalue_1, nvalue_2, res)
}
输出:
0.9 + 1.0 = 1.9