Golang 如何找到一个给定弧度值的余弦

Golang 如何找到一个给定弧度值的余弦

在本教程中,我们将学习如何在Golang编程语言中找到一个给定弧度值的余弦。Golang语言有许多带有预定义功能的包,开发者可以使用这些包而无需编写完整的逻辑。

为了执行数学运算和逻辑,我们在Golang中有一个 math 包。我们将只使用这个包来寻找给定弧度值的余弦。我们还将看到如何导入这个包,以及如何通过编写Golang代码来调用这个包中的一个函数。

余弦

定义

余弦是一个属于三角学的函数。为了理解余弦,请观察下面的图表。

如何在Golang中找到一个给定弧度值的余弦?

如果我们在上图的帮助下定义余弦。余弦函数的角度Ө等于邻边和斜边的比率。

cosӨ=邻边/斜边

不同角度下的余弦值

  • cos(0) = 1

  • cos(30) = √3 / 2

  • cos(45) = 1 / √2

  • cos(60) = 1 / 2

  • cos(90) = 0

  • cos(120) = -1 / 2

  • cos(135) = – 1 / √2

  • cos(150)=-√3 / 2

  • cos(180) = – 1

图形

现在我们将看到余弦函数的图形,观察图形上的上述数值。角度为0时,数值为1,直到角度变为90度时,数值为0。然后再到180度,我们将在第四象限得到一个镜像。

如何在Golang中找到一个给定弧度值的余弦?

算法

第1步 - 声明变量以存储监护人和答案的float32类型的值。

第2步 - 初始化弧度的变量。

第3步 --调用余弦函数并传递弧度值。

第4步 – 打印结果。

例子

在这个例子中,我们将写一个Golang程序,其中我们将导入一个 math 包并调用余弦函数。

package main
import (

   // fmt package provides the function to print anything
   "fmt"

   // math package provides multiple functions for different

   // mathematical operations
   "math"
)
func main() {

   // declaring the variables to store the value of radian value and answer
   var radianValue, answer float64
   fmt.Println("Program to find the cosine of a given radian value in the Golang programming language using a math package.")

   // initializing the value of radian value
   radianValue = 4.5

   // finding cosine for the given radian value
   answer = math.Cos(radianValue)

   // printing the result
   fmt.Println("The cosine value with the value of radian", radianValue, "is", answer)
} 

输出

Program to find the cosine of a given radian value in the Golang programming language using a math package.
The cosine value with the value of radian 4.5 is -0.21079579943077972

算法

第1步 - 声明变量以存储监护人和答案的float32类型的值。

第2步 - 初始化弧度的变量。

第3步 - 调用我们定义的余弦函数,并将弧度值作为参数。

第4步 – 打印结果。

例子

在这个例子中,我们将写一个Golang程序,其中我们将导入一个 math 包,并在一个单独的函数中调用余弦函数,并调用该函数main。

package main
import ( 

   // fmt package provides the function to print anything
   "fmt"

   // math package provides multiple functions for different

   // mathematical operations
   "math"
)

// this is a function with a parameter of float64 type and a return type of float64
func Cosine(angle float64) float64 {

   // returning the cosine of the angle
   return math.Cos(angle)
}
func main() {

   // declaring the variables to store the value of radian value and answer
   var radianValue, answer float64
   fmt.Println("Program to find the cosine of a given radian value in the Golang programming language using a separate function in the same program.")

   // initializing the value of the radian value
   radianValue = 4.5

   // finding cosine for the given radian value in separate function
   answer = Cosine(radianValue)

   // printing the result
   fmt.Println("The cosine value with the value of radian", radianValue, "is", answer)
} 

输出

Program to find the cosine of a given radian value in the Golang programming language using a separate function in the same program.
The cosine value with the value of radian 4.5 is -0.21079579943077972

结论

这是两种通过使用 math 包中的函数和传递弧度值作为参数来求余弦的方法。第二种方法将在程序中提供抽象性。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程