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

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

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

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

弧形余弦

定义

弧形余弦是一个与三角函数类似的函数。弧形余弦等于x的逆值。弧形余弦的公式写在下面,弧形余弦的值是在不同的角度。

语法

X = cos^-1(Ө)

图表

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

不同角度下的弧形余弦值

  • cos^-1(1) = 0

  • cos^-1(√3/2) = ㄫ / 6

  • cos^-1(1/√2) = ㄫ / 4

  • cos^-1(1/2) = ㄫ / 3

  • cos^-1(0) = ㄫ / 2

算法

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

第2步 --初始化该变量的值。

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

第4步 - 打印结果。

例子

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

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 \value and answer
   var value, answer float64
   fmt.Println("Program to find the Arc Cosine of a given value in the Golang programming language using a math package.")

   // initializing the value of the variable value
   value = 0

   // finding Arc Cosine for the given value
   answer = math.Acos(value)

   // printing the result 
   fmt.Println("The Arc Cosine value with the value of", value, "is", answer)
} 

输出

Program to find the Arc Cosine of a given value in the Golang programming language using a math package.
The Arc Cosine value with the value of 0 is 1.5707963267948966

算法

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

第2步 - 初始化该变量的值。

第3步 – 调用我们定义的弧形余弦函数,并将值作为参数传递。

第4步 – 打印结果。

例子

在这个例子中,我们将写一个Golang程序,其中我们将导入一个 数学 包,并在一个单独的函数中调用弧形余弦函数,并调用该函数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 ArcCosine(angle float64) float64 {

   // returning the Arc Cosine of the angle
   return math.Acos(angle)
}
func main() {

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

   // initializing the value of the value
   value = 0

   // finding Arc Cosine for the given value in a separate function
   answer = ArcCosine(value)

   // printing the result
   fmt.Println("The Arc Cosine value with the value of", value, "is", answer)
}

输出

Program to find the Arc Cosine of a given value in the Golang programming language using a separate function in the same program.
The Arc Cosine value with the value of 0 is 1.5707963267948966

结论

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

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程