Scala - Math pow() Method
The Scala Math pow() method returns the base raise to the power of exponent. In special cases it returns the following:
- If the exponent is zero, then the result is 1.0.
- If the exponent is NaN, then the result is NaN.
- If the base is NaN and the exponent is nonzero, then the result is NaN.
- If the exponent is 1.0, then the result is the same as the base.
Syntax
def pow(base: Double, exponent: Double): Double = java.lang.Math.pow(base, exponent)
Parameters
base |
Specify the base. |
exponent |
Specify the exponent. |
Return Value
Returns the base raise to the power of exponent.
Exception
NA.
Example:
In the example below, pow() method is used to calculate the base raised to the power of exponent.
import scala.math._ object MainObject { def main(args: Array[String]) { println(s"pow(10, 2) = ${pow(10, 2)}"); println(s"pow(5.2, 3) = ${pow(5.2, 3)}"); println(s"pow(5.2, -3) = ${pow(5.2, -3)}"); println(s"pow(5, Double.NaN) = ${pow(5, Double.NaN)}"); println(s"pow(Double.NaN, 2) = ${pow(Double.NaN, 2)}"); println("pow(2, Double.PositiveInfinity) = " + pow(2, Double.PositiveInfinity)); println("pow(2, Double.NegativeInfinity) = " + pow(2, Double.NegativeInfinity)); } }
The output of the above code will be:
pow(10, 2) = 100.0 pow(5.2, 3) = 140.608 pow(5.2, -3) = 0.007111970869367318 pow(5, Double.NaN) = NaN pow(Double.NaN, 2) = NaN pow(2, Double.PositiveInfinity) = Infinity pow(2, Double.NegativeInfinity) = 0.0
❮ Scala - Math Methods