如何在Go语言中查找给定弧度值的双曲正切值?
在本教程中,我们将学习如何在Go编程语言中查找给定弧度值的双曲正切值。Go语言拥有许多包含预定义函数的包,开发者可以使用这些函数而无需编写完整的逻辑。
为了执行数学运算和逻辑,Go语言中有一个math包。我们将只使用这个包来查找给定弧度值的双曲正切值。我们还将了解如何导入该包以及如何通过编写Go代码来调用该包中包含的函数。
双曲正切
定义
双曲正切是一个类似于三角函数的函数。代数表达式有助于找到包含指数函数(e^x)的双曲函数。双曲正切的公式如下所示,不同角度下双曲正切的值也列出。
语法
Tanhx = (ex – e-x) / (ex + e-x)
图像

不同角度下双曲正切的值
Tanh(0) = 0
Tanh(30) = 1
Tanh(45) = 1
Tanh(60) = 1
Tanh(90) = 1
算法
步骤1 - 声明变量以存储弧度值和float32类型的答案。
步骤2 - 初始化弧度变量。
步骤3 - 调用双曲正切函数并传入弧度值。
步骤4 - 打印结果。
示例
在这个示例中,我们将编写一个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 hyperbolic Tangent of a given radian value in the Golang programming language using a math package.")
// initializing the value of radian value
radianValue = 4.5
// finding Hyperbolic Tangent for the given radian value
answer = math.Tanh(radianValue)
// printing the result
fmt.Println("The hyperbolic Tangent value with the value of radian", radianValue, "is", answer)
}
输出
Program to find the hyperbolic Tangent of a given radian value in the Golang programming language using a math package. The hyperbolic Tangent value with the value of radian 4.5 is 0.9997532108480275
算法
步骤1 - 声明变量以存储弧度值和float32类型的答案。
步骤2 - 初始化弧度变量。
步骤3 - 调用我们定义的双曲正切函数并将弧度值作为参数传递。
步骤4 - 打印结果。
示例
在这个示例中,我们将编写一个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 HyperbolicTangent(angle float64) float64 {
// returning the Hyperbolic Tangent of the angle
return math.Tanh(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 Hyperbolic 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 hyperbolic Tangent for the given radian value in separate function
answer = HyperbolicTangent(radianValue)
// printing the result
fmt.Println("The Hyperbolic Tangent value with the value of radian", radianValue, "is", answer)
}
输出
Program to find the Hyperbolic Tangent of a given radian value in the Golang programming language using a separate function in the same program. The Hyperbolic Tangent value with the value of radian 4.5 is 0.9997532108480275
结论
这是两种使用math包中的函数并将弧度值作为参数传递来查找双曲正切的方法。第二种方法将为程序提供抽象。要了解更多关于Go语言的信息,您可以浏览这些教程。
广告
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP