What is Math.tanh() in Scala?

The tanh() function returns the hyperbolic tangent of a number. 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

The scala.math._ header file is required for this function.

Syntax

Double tanh(Double number)

Parameter

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

You can use the following formula to convert degrees to radians.

radians = degrees * ( Pi / 180 )

Return value

tanh() returns the hyperbolic tangent of a numberin radians that is sent as a parameter.

  • If the parameter value is positive infinity, then it returns 1.
  • If the parameter value is negative infinity, then it returns -1.
  • If the parameter value is NaN, then it returns NaN.

Example

import scala.math._
object Main extends App {
//positive number in radians
println(s"The value of tanh(2.3) = ${tanh(2.3)}");
// negative number in radians
println(s"The value of tanh(-2.3) = ${tanh(-2.3)}");
//converting the degrees angle into radians and then applying tanh()
// degrees = 45.0
// PI = 3.14159265
// result first converts degrees to radians then apply tanh
println(s"The value of tanh(45.0) = ${tanh(45.0 * (Pi / 180.0))}");
//error outputs
println(s"The value of tanh(Double.PositiveInfinity) = ${tanh(Double.PositiveInfinity)}");
println(s"The value of tanh(Double.NegativeInfinity) = ${tanh(Double.NegativeInfinity)}");
println(s"The value of tanh(Double.NaN) = ${tanh(Double.NaN)}");
}

Free Resources