Golang 如何找到给定值的反正切

Golang 如何找到给定值的反正切

在本教程中,我们将学习如何在Golang编程语言中找到给定值的反正切。Golang语言有很多预定义函数的包,开发者可以使用这些函数而不需要编写完整的逻辑。

为了进行数学运算和逻辑操作,我们在Golang中有一个 math 包。我们将仅使用这个包来找到给定值的反正切。我们还将看到如何导入该包以及如何通过编写Golang代码来调用该包中的函数。

反正切

定义

反正切是与三角函数类似的函数。反正切等于值x的倒数。反正切的公式如下,并且在不同的角度下取不同的反正切值。

语法

X = tan^-1(Ө)

图形

Golang 如何找到给定值的反正切

不同角度的反正切值

  • tan-1(1) = 0

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

  • tan-1(1/√2) = ㄫ / 4

  • tan-1(1/2) = ㄫ / 3

  • tan-1(0) = ㄫ / 2

步骤

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

   // initializing the value of the variable value
   value = 1

   // finding Arc Tangent for the given value 
   answer = math.Atan(value)

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

输出

Program to find the Arc Tangent of a given value in the Golang programming language using a math package.
The Arc Tangent value with the value of 1 is 0.7853981633974483

步骤

步骤1 - 声明变量以存储guardian和answer的值,类型为float32。

步骤2 - 初始化一个数值变量。

步骤3 - 调用我们定义的Arc Tangent函数,并将该数值作为参数传递。

步骤4 - 打印结果。

示例

在此示例中,我们将编写一个Golang程序,在该程序中我们将导入一个 math 包,并在一个单独的函数中调用Arc Tangent函数,并调用该函数为main。

package main
import (

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

   // math package provides multiple functions for different

   // mathematical operations
   "math"
)
type of float64
func ArcTangent(angle float64) float64 {

   // returning the Arc Tangent of the angle
   return math.Atan(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 Tangent of a given value in the Golang programming language using a separate function in the same program.")

   // initializing the value of the value
   value = 1

   // finding Arc Tangent for the given value in a separate function
   answer = ArcTangent(value)

   // printing the result
   fmt.Println("The Arc Tangent value with the value of", value, "is", answer)
}
Program to find the Arc Tangent of a given value in the Golang programming language using a separate function in the same program.
The Arc Tangent value with the value of 1 is 0.7853981633974483

结论

这是通过使用 math 包中的函数并将值作为参数传递来找到反正切的两种方法。第二种方法将提供程序中的抽象化。要了解更多关于Golang的信息,您可以探索这些教程。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程