What is Math.tanh() in Ruby?

The tanh() function in Ruby 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: This function is present in the Math module.

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 (radians) number that is sent as a parameter.

Example

#positive number in radians
puts "The value of tanh(2.3) : ", Math.tanh(2.3)
#negative number in radians
puts "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 * (Math::PI / 180.0)
puts "The value of tanh(#{result}) : ", Math.tanh(result)

Free Resources