Golang 如何查找数的正弦和余弦值
Go语言的math包提供了内置的基本常量和数学函数,用于对数进行操作。通过使用 Sincos()函数 ,您可以查找指定数的正弦和余弦值。这个函数是由math包提供的。因此,您需要使用import关键字在程序中添加math包以访问Sincos()函数。
语法:
func Sincos(a float64) (sin, cos float64)
- 如果您在此函数中传递-In f或+Inf,例如Sincos(-Inf)或Sincos(+ Inf),则此函数将返回NaN,NaN。
- 如果你在这个函数中传递-0或+0,例如Sincos(-0)或Sincos(+0),那么这个函数将返回-0或+0, 1。
- 如果您在此函数中传递NaN,例如Sincos(NaN),则此函数将返回NaN,NaN。
示例1:
// Golang程序示例,演示Sincos()函数
package main
import (
"fmt"
"math"
)
// 主函数
func main() {
// 使用Sincos()函数
res_1,res_2 := math.Sincos(math.Pi / 3)
res_3,res_4 := math.Sincos(0)
res_5,res_6 := math.Sincos(math.Inf(-1))
res_7,res_8 := math.Sincos(math.NaN())
res_9,res_10 := math.Sincos(math.Pi)
// 显示结果
fmt.Printf("结果1:%.1f, %.1f", res_1, res_2)
fmt.Printf("\n结果2:%.1f, %.1f", res_3, res_4)
fmt.Printf("\n结果3:%.1f, %.1f", res_5, res_6)
fmt.Printf("\n结果4:%.1f, %.1f", res_7, res_8)
fmt.Printf("\n结果5:%.1f, %.1f", res_9, res_10)
}
输出:
结果1:0.9,0.5
结果2:0.0,1.0
结果3:NaN,NaN
结果4:NaN,NaN
结果5:0.0,-1.0
示例2:
// Golang程序示例,演示Sincos()函数
package main
import (
"fmt"
"math"
)
// 主函数
func main() {
// 使用Sincos()函数
nvalue_1,nvalue_2 := math.Sincos(math.Pi / 2)
nvalue_3,nvalue_4 := math.Sincos(math.Pi / 3)
// 给定正弦值的总和
res1 := nvalue_1 + nvalue_3
fmt.Println("正弦值的总和:")
fmt.Printf("%.2f + %.2f = %.2f\n",
nvalue_1, nvalue_3, res1)
// 给定余弦值的总和
res2 := nvalue_2 + nvalue_4
fmt.Println("余弦值的总和:")
fmt.Printf("%.2f + %.2f = %.2f",
nvalue_2, nvalue_4, res2)
}
输出:
正弦值的总和:
1.00 + 0.87 = 1.87
余弦值的总和:
0.00 + 0.50 = 0.50
极客教程