如何在Go语言中查找给定弧度值的正切?
给定弧度的正切是指其邻边与对边的比率。Go语言拥有许多包含预定义函数的包,开发者可以使用这些函数而无需编写完整的逻辑。为了执行数学运算和逻辑,Go语言中有一个math包。我们将仅使用此包来查找给定弧度值的正切。我们还将了解如何导入该包以及如何通过编写Go代码来调用该包中包含的函数。
正切
定义
正切是三角函数的一部分。要理解正切,请观察下图。
如果我们借助上图定义正切,则角度$\theta$的正切函数等于对边与邻边的比率。

tan$\theta$ = 对边 / 邻边
不同角度下的正切值
tan(0) = 0
tan(30) = 1 / √3
tan(45) = 1
tan(60) = √3
tan(90) = 未定义
tan(120) = − √3
tan(135) = − 1
tan(150) = −1 / √3
tan(180) = 0
图形
现在我们将看到正切函数的图形,并在图形上观察上述值。对于零角度,值为零,然后直到角度达到90度,值为未定义。然后,直到180度,我们将得到第四象限的镜像。
算法
步骤 1 − 声明变量以存储弧度值和float32类型的答案。
步骤 2 − 初始化弧度变量。
步骤 3 − 调用正切函数并传递弧度值。
步骤 4 − 打印结果。
示例 1
在这个例子中,我们将编写一个Go程序,在其中我们将导入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 Tangent of a given radian value in the Golang programming language using a math package.")
// initializing the value of the radian value
radianValue = 4.5
// finding tangent for the given radian value
answer = math.Tan(radianValue)
// printing the result
fmt.Println("The Tangent value with the value of radian", radianValue, "is", answer)
}
输出
Program to find the Tangent of a given radian value in the Golang programming language using a math package. The Tangent value with the value of radian 4.5 is 4.637332054551185
示例 2
在这个例子中,我们将编写一个Go程序,在其中我们将导入math包,在一个单独的函数中调用正切函数,并在主函数中调用该函数。
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 Tangent(angle float64) float64 {
// returning the Tangent of the angle
return math.Tan(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 Tangent 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 factorial of n
answer = Tangent(radianValue)
// finding tangent for the given radian value in the separate function
fmt.Println("The Tangent value with the value of radian", radianValue, "is", answer)
}
输出
Program to find the Tangent of a given radian value in the Golang programming language using a separate function in the same program. The Tangent value with the value of radian 4.5 is 4.637332054551185
结论
这两种方法都是通过使用math包中的函数并将弧度值作为参数传递来查找正切。如果我们比较这两种方法,则创建单独函数的第二种方法将通过创建单独的函数来在程序中提供抽象,并且可以在不同的地方重复使用。要了解更多关于Go语言的信息,您可以浏览这些教程。
广告
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP