What is the exponent operator in Kotlin. I assumed it would be **
but it seems to throw up an error in my code.
when (pendingOperation) {
\"
Kotlin, like Java, does not have an exponent operator. Java has Math.pow
, which you can use with Kotlin too, but Kotlin also has extension functions for Float and Double that you can use instead.
Should you need to use exponents with Ints or Longs, you just convert to double and back to int/long afterwards. Alternatively you can create your own methods.
It's pretty straight.forward since it's an extension function; just call .pow
on a Double
or Float
object:
"a^b" -> operand1 = operand1!!/*.toDouble()*/.pow(value)/*.toInt()*/
//Not sure what type operand1 is, so the comments are there if it's not a double or float, and the second assumes it's an int
Note: due to limitations of DEX, the rest of this answer DOES NOT work on Android. Use .pow()
or Math.pow()
instead. This still works on the JVM (and presumably Kotlin Native), but not Android.
You can, however, create some infix functions to kinda get one. The idea here is using the escape chars for Kotlin names to create a Python-style exponent operator. The escape chars are meant to escape Kotlin keywords in Java functions, but they allow for a lot of fun. You can also have names with spaces, assuming you wrap it in backticks.
You can also write your own power function entirely from scratch, but note that Math.pow()
's implementation is written in C++, and will most likely be faster than one written in Kotlin or Java.
/**
* Integer power using [Double.pow]
*/
infix fun Int.`**`(exponent: Int): Int = toDouble().pow(exponent).toInt()
/**
* Long power using [Double.pow]
*/
infix fun Long.`**`(exponent: Int): Long = toDouble().pow(exponent).toLong()
/**
* Double power using [Double.pow]
*/
infix fun Float.`**`(exponent: Int) : Float = this.pow(exponent)
/**
* Float power using [Float.pow]
*/
infix fun Double.`**`(exponent: Int) : Double = this.pow(exponent)
Which allows you to call:
val x = 10
val exponent = 2
println(x `**` exponent)
assertEquals(x `**` exponent, 100)
Notice the backticks (``
). As I mentioned earlier, in Kotlin, these are intended to escape java calls, when a function, variable, class, or something else contains a Kotlin keyword. For an instance, if I declare a boolean is()
in Java and want to call it from Kotlin, I'd have to call it as ContainingClass.`is`
.
However, they're not limited to use when calling Java functions or variables. You can use them as actual variable or function names. I.e. var `this`
could be a variable name, but has to be called as `this`
.
For an instance, this is perfectly valid code:
var `this` = 32
`this` += 10;
print(`this`)
... but if you run it in a class, and use print(this)
instead of using print(`this`)
, you print the result of the class' toString()
method instead of 42.
Additionally, the backticks do not care what's escaped. Kotlin keyword or not, everything is escaped. You can have spaces, symbols, emojis (so you can finally have that val
As other answers have mentioned there is no exponent operator in Kotlin/Java. However, it is not advisable to use the extension function available in Double and Float for performing exponentiation of Long. Here's why: Conversion of Double back to Long after exponentiation can lead to rounding of number due to precision limits of Double.
val EXP = 38
println(3.toDouble().pow(EXP).toLong())
// 1350851717672992000
println(3.toDouble().pow(EXP))
// 1.350851717672992E18
But the actual answer was 1350851717672992089. Thus, I'd advise you to use BigIntegers to perform exponentiation. The same can also be used for fast modulo exponentiation. Here's our final pow(base, exp)
function:
fun pow(n: Long, exp: Int): Long{
return BigInteger.valueOf(n).pow(exp).toLong()
}
use extension method pow
inline fun Double.pow(x: Double): Double (source)
for more detail pow
If you need a cross platform version that does not depend on java.lang
or Kotlin's Double.pow
, this simple implementation will work:
fun pow(value: Long, exp: Int): Long {
return when {
exp > 1 -> value * pow(value,exp-1)
exp == 1 -> value
else -> 1
}
}
Here's another way to solve this problem:
"a^b" -> operand1 = (operand1!! + 0.0).pow(value + 0.0).toInt()