📌  相关文章
📜  如何在 Golang 中找到一个数的 Sin 和 Cos 值?

📅  最后修改于: 2021-10-24 13:00:46             🧑  作者: Mango

Go 语言为基本常量和数学函数提供内置支持,以在 math 包的帮助下对数字执行运算。您可以借助math 包提供的Sincos()函数找到指定数字的 sin 和 cos 的值。因此,您需要在 import 关键字的帮助下在程序中添加一个数学包来访问 Sincos()函数。

句法:

func Sincos(a float64) (sin, cos float64)
  • 如果像 Sincos(-Inf) 或 Sincos(+Inf) 一样在此函数传递 -Inf 或 +Inf,则此函数将返回 NaN、NaN。
  • 如果像 Sincos(-0) 或 Sincos(+0) 一样在此函数传递 -0 或 +0,则此函数将返回 -0 或 +0, 1。
  • 如果你像 Sincos(NaN) 一样在这个函数传递 NaN,那么这个函数将返回 NaN,NaN。

示例 1:

// Golang program to illustrate the Sincos() Function
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Using Sincos() function
    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)
  
    // Displaying the result
    fmt.Printf("Result 1: %.1f, %.1f", res_1, res_2)
    fmt.Printf("\nResult 2: %.1f, %.1f", res_3, res_4)
    fmt.Printf("\nResult 3: %.1f, %.1f", res_5, res_6)
    fmt.Printf("\nResult 4: %.1f, %.1f", res_7, res_8)
    fmt.Printf("\nResult 5: %.1f, %.1f", res_9, res_10)
  
}

输出:

Result 1: 0.9, 0.5
Result 2: 0.0, 1.0
Result 3: NaN, NaN
Result 4: NaN, NaN
Result 5: 0.0, -1.0

示例 2:

// Golang program to illustrate the Sincos() Function
  
package main
  
import (
    "fmt"
    "math"
)
  
// Main function
func main() {
  
    // Using Sincos() function
    nvalue_1, nvalue_2 := math.Sincos(math.Pi / 2)
    nvalue_3, nvalue_4 := math.Sincos(math.Pi / 3)
  
    // Sum of the given sin values
    res1 := nvalue_1 + nvalue_3
    fmt.Println("Sum of Sin Values:")
    fmt.Printf("%.2f + %.2f = %.2f\n", 
            nvalue_1, nvalue_3, res1)
  
    // Sum of the given cos values
    res2 := nvalue_2 + nvalue_4
    fmt.Println("Sum of Cos Values:")
    fmt.Printf("%.2f + %.2f = %.2f", 
           nvalue_2, nvalue_4, res2)
  
}

输出:

Sum of Sin Values:
1.00 + 0.87 = 1.87
Sum of Cos Values:
0.00 + 0.50 = 0.50