4

I have read What is the Kotlin exponent operator and try to follow it's answer by writing val t1 = 23.0 then print (t1!!.pow(4.9)) in Android Studio REPL Kotlin mode and get for second error: unresolved reference: pow. The code which is in other places found by search for Kotlin power function works fine print (Math.pow(t1,4.0)). I was puzzled but found that post Unresolved reference: pow in Eclipse using Kotlin and when I did import kotlin.math.pow next print (t1.pow(4.9)) started to give a number. Also I noted import kotlin.Math.pow gives error: unresolved reference: Math, so
1. Why Math.pow but import kotlin.math?
2. Could I use extension function pow w/out importing in REPL something like t1.math.pow(2.3) (as it is gives error: unresolved reference: math?

Marisha
  • 816
  • 9
  • 14

1 Answers1

7

When using Math.pow(10.0, 2.0)

println(Math.pow(10.0, 2.0)) // "100.0"

Math refers to the java.lang.Math class.

You can verify this by calling

println(Math::class) // "java.lang.Math"

In this case, nothing needs to be imported as the java.lang package is imported by default.


When using 10.0.pow(2)

println(10.0.pow(2)) // "100.0"

pow refers to fun Double.pow(x: Double): Double, the Kotlin extension function that needs to be explicitly imported from kotlin.math.pow:

import kotlin.math.pow
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156