Scala Tutorial Scala References

Scala - Math cosh() Method



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

cosh

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 positive or negative infinity, then the result is Infinity.
  • If the argument is zero, then the result is 1.0.

Syntax

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

Parameters

x Specify the value.

Return Value

Returns the hyperbolic cosine of a value.

Exception

NA.

Example:

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

import scala.math._

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

The output of the above code will be:

cosh(-1) = 1.543080634815244
cosh(0) = 1.0
cosh(1) = 1.543080634815244
cosh(2) = 3.7621956910836314
cosh(Double.NaN) = NaN
cosh(Double.PositiveInfinity) = Infinity
cosh(Double.NegativeInfinity) = Infinity

❮ Scala - Math Methods