How to find the Hyperbolic Tangent of a given radian value in Golang?


In this tutorial, we will learn how to find the Hyperbolic Tangent of a given radian value in the Golang programming Language. Golang language has many packages with predefined functions that the developer can use without writing the complete logic.

To perform the mathematical operations and logic we have a math package in Golang. We will use this package only to find the Hyperbolic Tangent of a given radian value. We will also see how to import the package and also how to call a function this package consists of by writing a Golang code.

Hyperbolic Tangent

Definition

Hyperbolic Tangent is a function similar to trigonometry functions. The algebraic expression helps in finding the hyperbolic functions that include the exponential function(e^x). The formula for hyperbolic Tangent is written below and the value of Hyperbolic Tangent is at different angles.

Syntax

Tanhx = (ex – e-x) / (ex + e-x)

Graph


Value of Hyperbolic Tangent at different angles

  • Tanh(0) = 0

  • Tanh(30) = 1

  • Tanh(45) = 1

  • Tanh(60) = 1

  • Tanh(90) = 1

Algorithm

Step 1 - Declaring the variable to store the value of the guardian and answer of float32 type.

Step 2 - Initializing the variable of a radian.

Step 3 - Call the function of the hyperbolic Tangent and pass the radian value.

Step 4 - Printing the result.

Example

In this example, we will write a Golang program in which we will import a math package and call the hyperbolic Tangent function.

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)
}

Output

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

Algorithm

Step 1 - Declaring the variable to store the value of the guardian and answer of float32 type.

Step 2 - Initializing the variable of a radian.

Step 3 - Call the function of the hyperbolic Tangent defined by us and pass the radian value as a parameter.

Step 4 - Printing the result.

Example

In this example, we will write a Golang program in which we will import a math package and call the hyperbolic Tangent function in a separate function and call that function 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 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)
} 

Output

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

Conclusion

These are two ways to find the hyperbolic Tangent by using the function in the math package and passing the radian value as a parameter. The second approach will provide abstraction in the program. To learn more about Golang you can explore these tutorials.

Updated on: 29-Nov-2022

92 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements