1. Overview

Kotlin, just like Java, does not have an exponent operator. However, it has a pow() extension function that we can call on a Float or Double.

In this tutorial, we’ll see various ways of performing the exponent operation in Kotlin.

2. Different Ways of Performing Exponent Operation in Kotlin

Let’s dive straightaway into the various ways we can calculate the power of a number in Kotlin.

2.1. Algorithmically Calculate the Power of a Number

One way we can achieve the power of a number in Kotlin is by developing an efficient algorithm that can help us achieve the desired results. Let’s take a look at a program that can do just that:

fun power(baseVal: Int, exponentVal: Int): Long {
    val base = baseVal
    var exponent = exponentVal
    var result: Long = 1

    while (exponent != 0) {
        result *= base.toLong()
        --exponent
    }
    return result
}
assertEquals(4, power(2,2))
assertEquals(1024, power(4, 5)

Basically, we multiply the base value by itself exponent number of times as long as it is not zero.

Another approach could be to calculate the power recursively:

fun power(baseVal: Int, exponentVal: Int): Long {
    if (exponentVal != 0) {
        return baseVal  * power(baseVal, exponentVal - 1)
    }
    else {
        return 1
    }
}
assertEquals(4, power(2,2))
assertEquals(1024, power(4, 5)

2.2. Using pow() Extension Function

As mentioned earlier, the pow() function is an extension function that can only be called on a Float or Double object. What this means is that to use this function on an Int or Long, we need to convert it first to a Double, then back to Int or Long afterward:

val baseDouble = 5.0
val exponentDouble = 2.5
assertEquals(55.9016994375, baseDouble.pow(exponentDouble))

val baseInt = 3
val exponentInt = -4
assertEquals(0.012345679012345678, baseInt.toDouble().pow(exponentInt.toDouble()))

In the first assertion statement, we do not convert the values because they are of type Double and, as such, are qualified to call the pow() function. For the second assert statement, this is not the case since the values have a type Int.

2.3. Using Math.pow() Function

Another interesting way we can calculate the power of a number is by using the Math.pow() function. Albeit this is originally a Java function from the java.lang library, we can still make use of this function in Kotlin:

assertEquals(55.90169943749474, Math.pow(5.0, 2.5))
assertEquals(0.012345679012345678, Math.pow(3.0, -4.0))

We notice that this function accepts parameters of type Double too. So, if we need to call this function on an Int or Long, we need to convert them to a Double first:

val baseInt = 3
val exponentInt = -4
assertEquals(0.012345679012345678, Math.pow(baseInt.toDouble(), exponentInt.toDouble()))

3. Conclusion

In this tutorial, we have seen several ways we can perform power operations on a Number in Kotlin. We also notice that other than the algorithmic approach, performing the power operation using the pow() or Math.pow() functions requires parameters of type Double or Float.

As usual, all code samples are available over on GitHub.