What is the Kotlin exponent operator

后端 未结 5 655
春和景丽
春和景丽 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:37

    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

提交回复
热议问题