Scala Tutorial Scala References

Scala - Math exp() Method



The Scala Math exp() method returns e raised to the power of specified number, i.e., ex. 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 zero.

Syntax

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

Parameters

x Specify the exponent of e.

Return Value

Returns e raised to the power of specified number.

Exception

NA.

Example:

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

import scala.math._

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

The output of the above code will be:

exp(-1) = 0.36787944117144233
exp(0) = 1.0
exp(1) = 2.718281828459045
exp(2) = 7.38905609893065
exp(Double.NaN) = NaN
exp(Double.PositiveInfinity) = Infinity
exp(Double.NegativeInfinity) = 0.0

❮ Scala - Math Methods