Golang 查找指定数字的余弦值

Golang 查找指定数字的余弦值

Go语言提供了内置支持基本常量和数学函数以执行有关数的操作,并使用math包提供的Cos()函数来查找指定弧度参数的余弦值。因此,您需要使用import关键字将math包添加到您的程序中,以访问Cos()函数。

语法:

func Cos(y float64) float64
  • 如果在此函数中传递+Inf或-Inf,则此函数将返回NaN(非数字)。
  • 如果在此函数中传递NaN,则此函数将返回NaN。

示例1:

// Golang program to illustrate how to find
// the cosine of the given radian argument

package main

import (
 "fmt"
 "math"
)

// Main function
func main() {

 // Finding the cosine
 // of the given value
 // Using Cos() function
 res_1 := math.Cos(math.Pi / 4)
 res_2 := math.Cos(math.Inf(-3))
 res_3 := math.Cos(0)
 res_4 := math.Cos(math.NaN())
 res_5 := math.Cos(math.Pi)

 // Displaying the result
 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: 1.0
Result 4: NaN
Result 5: -1.0

示例2:

// Golang program to illustrate how to find
// the cosine of the given radian argument

package main

import (
 "fmt"
 "math"
)

// Main function
func main() {

 // Finding cosine of
 // the given number
 nvalue_1 := math.Cos(0)
 nvalue_2 := math.Cos(math.Pi / 2)

 // Sum of the given values
 res := nvalue_1 + nvalue_2

 // Displaying results
 fmt.Printf("%.1f + %.1f = %.1f", nvalue_1, nvalue_2, res)

} 

输出:

1.0 + 0.0 = 1.0

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程