What is the Kotlin exponent operator

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



What is the Kotlin exponent operator



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) {
"=" -> operand1 = value
"÷" -> operand1 = if (value == 0.0)
Double.NaN // handle attempt to divide by zero
else
operand1!! / value

"x" -> operand1 = operand1!! * value
"−" -> operand1 = operand1!! - value
"+" -> operand1 = operand1!! + value
"a^b" -> operand1 = operand1!! ** value




2 Answers
2



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.


Math.pow



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:


.pow


Double


Float


"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



You can, however, create some infix functions to kinda get one:


infix fun


/**
* Integer power; manually created
*/
//infix fun Int.`**`(exponent: Int): Int
// var res = 1;
// for(i in exponent downTo 1)
// res *= this
//
// return res;
//
//
/**
* Long power: manually created
*/
//infix fun Long.`**`(exponent: Int) : Long
// var res = 1L;
// for(i in exponent downTo 1)
// res *= this
//
// return res;
//

/**
* 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)



There are two commented functions you can use as an alternative to Long and Integer power using manual calculation instead of Double.pow.



Which allows you to call:


val x = 10
val exponent = 2
println(x `**` exponent)
assertEquals(x `**` exponent, 100)



Notice the `` - in Kotlin these are used to escape keywords and use them as actual names. I.e. var `this` could be a variable name, but has to be called as `this`.


var `this`


`this`



If you don't know what the infix keyword is, it enables calling functions without periods and parenthesis. The reason it's used here is to make x `**` exponent an actual valid function call


infix


x `**` exponent



use extension method pow


inline fun Double.pow(x: Double): Double (source)



for more detail pow






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard