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 straightforward 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 Java functions with names colliding with Kotlin keywords, but they allow for a lot of fun. You can, for instance, 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]
* Note: it may be preferable to use a BigInteger instead of toDouble()
* to prevent a loss of precision - use whichever makes sense
* for the number you have at hand, and the precision you need.
*/
infix fun Long.`**`(exponent: Int): Long = toDouble().pow(exponent).toLong()
// infix fun Long.`**`(exponent: Int): Long = toBigInteger().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/dereferenced as `this`
.
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 `` = 42;
you've always wanted), keywords... That's what I've used in the function declaration. By declaring it with the escape characters, it makes a normally illegal name legal, presumably because it's interpreted as a raw string instead. I'm not entirely sure how it works under the hood, but it doesn't really matter when used. There's actually very few real uses for these aside interacting with Java-based libraries, but there's a lot of fun to be had too.
The second part of what makes the function work is the infix
keyword. If you don't know what the infix
keyword is, it enables calling functions without periods and parentheses. The reason it's used here is to make x `**` exponent
an actual valid function call - without it, the code would be field.`**`(2)
. You can read more about infix functions in the documentation
You can also pick a different name to get rid of the backticks - I just used **
, mainly because it's Python-like. It's also used in JavaScript and presumably some other languages as well. Other illegal names can also be picked by using backticks.