What is Math.exp() in Scala?

The exp() function returns the mathematical e raised to the power of a specific number. Here, Euler’s number e is the base of the natural logarithm.

Figure 1 shows the mathematical representation of the exp() function.

Figure 1: Mathematical representation of the exp() function

The following module is required for this function.

import scala.math._

Syntax

def exp(x: Double): Double

Parameter

This function requires a number as a parameter.

Return value

exp() returns the mathematical e raised to the power of the specific number sent as a parameter.

If the parameter value is PositiveInfinity, then it returns positive infinity.

If the parameter value is NegativeInfinity, then it returns zero.

If the parameter value is NaN, then it returns NaN.

Code

import scala.math._
object Main extends App {
//positive number
println(s"The value of exp(10) = ${exp(10)}");
//negative number
println(s"The value of exp(-2) = ${exp(-2)}");
//zero number
println(s"The value of exp(0) = ${exp(0)}");
//error outputs
println(s"The value of exp(Double.PositiveInfinity) = ${exp(Double.PositiveInfinity)}");
println(s"The value of exp(Double.NegativeInfinity) = ${exp(Double.NegativeInfinity)}");
println(s"The value of exp(Double.NaN) = ${exp(Double.NaN)}");
}

Free Resources