What is the Tanh function in Golang?

The Tanh function in the Go programming language is used to find the hyperbolic tangent value of the angle passed to it as input.

To use the Tanh function, you must import the math package in your file and use the . notation (math.Tanh) to access the Tanh function. Here, Tanh is the actual function, while math is the Go package that stores the definition of this function.

Syntax

The definition of the Tanh function inside the math package is shown below:

Parameters

The Tanh function takes a single argument of type float64 that represents the angle whose hyperbolic tangent value you want to find.

Return value

The Tanh function returns a single value of type float64 that represents the hyperbolic tangent value of the argument. The possible range of all return values is between -1 and +1.

An exception to the above statements is when you pass something that is NAN as an argument. In these cases, the Tanh function returns a NAN.

Examples

Below is a simple example where we use the Tanh function to find the hyperbolic tangent value of 25:

package main
import "fmt"
import "math"
func main() {
x := math.Tanh(25)
fmt.Println(x)
}

The following example shows how passing a NAN as an argument makes the Tanh function return NAN:

package main
import "fmt"
import "math"
func main() {
x := math.Tanh(math.NaN())
fmt.Println(x)
}

In the example above, we use the NaN function to generate a NAN value.

New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources