Scala Tutorial Scala References

Scala - Math tanh() Method



The Scala Math tanh() method returns hyperbolic tangent of a value. The hyperbolic tangent of x is defined as:

tanh

where e is an Euler's number.

In special cases it returns the following:

  • If the argument is NaN, then the result is NaN.
  • If the argument is zero, then the result is a zero.
  • If the argument is positive infinity, then the result is +1.0.
  • If the argument is negative infinity, then the result is -1.0.

Syntax

def tanh(x: Double): Double = scala.lang.Math.tanh(x)

Parameters

x Specify the value.

Return Value

Returns the hyperbolic tangent of a value.

Exception

NA.

Example:

In the example below, tanh() method is used to find out the hyperbolic tangent of a value.

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"tanh(-1) = ${tanh(-1)}");   
    println(s"tanh(0) = ${tanh(0)}"); 
    println(s"tanh(1) = ${tanh(1)}"); 
    println(s"tanh(2) = ${tanh(2)}"); 
    println(s"tanh(Double.NaN) = ${tanh(Double.NaN)}"); 
    println("tanh(Double.PositiveInfinity) = "
            + tanh(Double.PositiveInfinity));
    println("tanh(Double.NegativeInfinity) = "
            + tanh(Double.NegativeInfinity));
  }
}

The output of the above code will be:

tanh(-1) = -0.7615941559557649
tanh(0) = 0.0
tanh(1) = 0.7615941559557649
tanh(2) = 0.9640275800758169
tanh(Double.NaN) = NaN
tanh(Double.PositiveInfinity) = 1.0
tanh(Double.NegativeInfinity) = -1.0

❮ Scala - Math Methods