Golang math.Tanh() Function with Examples

Golang | math.Tanh() Function: Here, we are going to learn about the Tanh() function of the math package with its usages, syntax, and examples.
Submitted by IncludeHelp, on September 04, 2021

math.Tanh()

The Tanh() function is an inbuilt function of the math package which is used to get the hyperbolic tangent of the given value.

It accepts a parameter (x) and returns the hyperbolic tangent of x.

Syntax:

func Tanh(x float64) float64

Parameter(s):

  • x : The value whose hyperbolic tangent is to be found.

Return Value:

The return type of Tanh() function is a float64, it returns the hyperbolic tangent of the given value.

Special cases:

  • Tanh(±0) = ±0
    If the parameter is ±0, the function returns the same value (±0).
  • Tanh(±Inf) = ±1
    If the parameter is ±Inf, the function returns ±1.
  • Tanh(NaN) = NaN
    If the parameter is NaN, the function returns the same value (NaN).

Example 1:

// Golang program to demonstrate the
// example of math.Tanh() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	fmt.Println(math.Tanh(1))
	fmt.Println(math.Tanh(0.23))
	fmt.Println(math.Tanh(-1))
	fmt.Println(math.Tanh(-0.23))
	fmt.Println(math.Tanh(-1.23))
	fmt.Println(math.Tanh(1.23))
	fmt.Println(math.Tanh(math.Pi))
	fmt.Println(math.Tanh(0))
	fmt.Println(math.Tanh(math.Inf(1)))
	fmt.Println(math.Tanh(math.Inf(-1)))
	fmt.Println(math.Tanh(math.NaN()))
}

Output:

0.7615941559557649
0.22602835227867096
-0.7615941559557649
-0.22602835227867096
-0.8425793256589296
0.8425793256589296
0.99627207622075
0
1
-1
NaN

Example 2:

// Golang program to demonstrate the
// example of math.Tanh() Function

package main

import (
	"fmt"
	"math"
)

func main() {
	var x float64
	var TanhX float64

	x = 0
	TanhX = math.Tanh(x)
	fmt.Printf("Hyperbolic tangent of %.2f is %.2f\n", x, TanhX)

	x = 0.23
	TanhX = math.Tanh(x)
	fmt.Printf("Hyperbolic tangent of %.2f is %.2f\n", x, TanhX)

	x = 1
	TanhX = math.Tanh(x)
	fmt.Printf("Hyperbolic tangent of %.2f is %.2f\n", x, TanhX)

	x = -0.23
	TanhX = math.Tanh(x)
	fmt.Printf("Hyperbolic tangent of %.2f is %.2f\n", x, TanhX)

	x = -2
	TanhX = math.Tanh(x)
	fmt.Printf("Hyperbolic tangent of %.2f is %.2f\n", x, TanhX)

	x = math.Pi
	TanhX = math.Tanh(x)
	fmt.Printf("Hyperbolic tangent of %.2f is %.2f\n", x, TanhX)
}

Output:

Hyperbolic tangent of 0.00 is 0.00
Hyperbolic tangent of 0.23 is 0.23
Hyperbolic tangent of 1.00 is 0.76
Hyperbolic tangent of -0.23 is -0.23
Hyperbolic tangent of -2.00 is -0.96
Hyperbolic tangent of 3.14 is 1.00

Golang math Package Constants and Functions »




Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.