By Alvin Alexander. Last updated: March 13, 2024
I was just working on a math problem in Scala where I needed to get the exponent of a value, and after some research, I found that the right approach is to use the scala.math.pow (see scala.math) function when you want the power/exponent of a value. Here's the output from a REPL command:
scala> scala.math.pow(2, 3) res1: Double = 8.0
I was originally using the Math.pow function, and getting a deprecation warning:
scala> Math.pow(2, 3) warning: there were 1 deprecation warnings; re-run with -deprecation for details res2: Double = 8.0
So it looks like using the scala.math.pow function (the first example shown) is the correct approach here.
Note: the “power” is the second parameter
Also note that the second parameter is the “power” value. Here are a few more examples to demonstrate this:
scala.math.pow(2, 2) // 4.0 scala.math.pow(2, 3) // 8.0 scala.math.pow(2, 4) // 16.0 scala.math.pow(3, 2) // 9.0 scala.math.pow(3, 3) // 27.0
Also note that both parameters to pow
are Double
values, and pow
returns a Double
.