What is Math.pow() in Scala?

The pow() function calculates the value of the numberbase raised to the power of another numberexponent.

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

Figure 1: Mathematical representation of pow() function

The following module is required for this function:

import scala.math._

Syntax

Double pow(Double baseNumber, Double exponent)

Parameters

The pow() function requires two parameters:

  • A base number
  • An exponent number

Return value

pow() returns the value of the base number raised to the exponent number.

  • If the parameter value of the base, exponent, or both is positive infinity, then it returns positive infinity.
  • If the parameter value of the base, exponent, or both is negative infinity, then it returns zero.
  • If the parameter value of the base, exponent, or both is NaN, then it returns NaN.

Example

import scala.math._
object Main extends App {
//positive: baseNumber positive: exponent
println(s"The value of pow(2,3) = ${pow(2,3)}");
//positive: baseNumber negative: exponent
println(s"The value of pow(0.25,-2) = ${pow(0.25,-2)}");
//negative: baseNumber positive: exponent
println(s"The value of pow(-10,2) = ${pow(-10,2)}");
//negative: baseNumber negative: exponent
println(s"The value of pow(-0.5,-1) = ${pow(-0.5,-1)}");
//error ouput
println(s"The value of pow(Double.PositiveInfinity,Double.PositiveInfinity) = ${pow(Double.PositiveInfinity,Double.PositiveInfinity)}");
println(s"The value of pow(Double.NegativeInfinity,Double.NegativeInfinity) = ${pow(Double.NegativeInfinity,Double.NegativeInfinity)}");
println(s"The value of pow(Double.NaN,Double.NaN) = ${pow(Double.NaN,Double.NaN)}");
}

Free Resources