Scala Tutorial Scala References

Scala - Math expm1() Method



The Scala Math expm1() method returns e raised to the power of specified number minus 1, i.e., ex-1. Please note that e is the base of the natural system of logarithms, and its value is approximately 2.718282. In special cases it returns the following:

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

Syntax

def expm1(x: Double): Double = java.lang.Math.expm1(x)

Parameters

x Specify the exponent of e.

Return Value

Returns e raised to the power of specified number minus 1, i.e., ex-1.

Exception

NA.

Example:

In the example below, expm1() method is used to calculate e raised to the power of specified number minus one.

import scala.math._

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

The output of the above code will be:

expm1(-1) = -0.6321205588285577
expm1(0) = 0.0
expm1(1) = 1.718281828459045
expm1(2) = 6.38905609893065
expm1(Double.NaN) = NaN
expm1(Double.PositiveInfinity) = Infinity
expm1(Double.NegativeInfinity) = -1.0

❮ Scala - Math Methods

5