What is math.tanh() in Python?

The tanh() function in Python returns a number’s hyperbolic tangent. To be more specific, it returns the hyperbolic tangent of a number in radians.

Figure 1 shows the mathematical representation of the tanh() function.

Figure 1: Mathematical representation of the hyperbolic tangent function

Note: The math module is required for this function.

Syntax

tanh(num)

Parameter

This function requires a number that represents an angle in radians as a parameter.

To convert degrees to radians, use:

radians = degrees * ( PI / 180.0 )

Return value

tanh() returns the hyperbolic tangent of a number (radians) that is set as a parameter.

Example

import math
#positive number in radians
print "The value of tanh(2.3) : ", math.tanh(2.3)
#negative number in radians
print "The value of tanh(-2.3) : ", math.tanh(-2.3)
#converting the degrees angle into radians and then applying tanh()
#degrees = 45
#PI = 3.14159265
result = 45.0 * (math.pi / 180.0)
print "The value of tanh("+str(result)+") : ", math.tanh(result)