What is the Kotlin exponent operator

后端 未结 5 656
春和景丽
春和景丽 2021-01-07 17:33

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) {
    \"         


        
5条回答
  •  抹茶落季
    2021-01-07 17:48

    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
        }
    }
    

提交回复
热议问题