Kotlin pow()

abereFejiro9953409650's avatar
Published Oct 24, 2023
Contribute to Docs

The pow() method in Kotlin’s math class is used to calculate a number raised to the power of another number. It takes two parameters - the base (number to be raised to a power) and the exponent (the power to which the base is raised). The base and component can be either a double or float but not an integer.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours

Syntax

import kotlin.math.*
fun base: Double.pow(exponent: Double): Double
fun base: Double.pow(exponent: Float): Float
  • base: The base number.
  • exponent: The number that raises the base number.

The pow() method returns a value of type double or float.

Example

This example shows how use the pow() method to calculate 5.0 raised to the power of 2.5:

import kotlin.math.pow
fun main() {
val base = 5.0
val exponent = 2.5
val result = base.pow(exponent)
println("$base raised to the power of $exponent is $result.")
}

The code above will result in the following output:

5.0 raised to the power of 2.5 is 55.90169943749474.

All contributors

Contribute to Docs

Learn Kotlin on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn Kotlin, the expressive, open-source programming language developed by JetBrains.
    • Beginner Friendly.
      9 hours