Library contents

Kotlin kotlin.math.cbrt

In Kotlin, kotlin.math.cbrt returns the real cube root. The cube root of −8 is −2 because (−2) × (−2) × (−2) = −8.

A simple example

kotlin.math.cbrt

  1. Start with 8
  2. Do this Find the cube root
  3. Result 2
Two multiplied by itself three times gives 8.
Lesson.kt
fun main() {
    println(kotlin.math.cbrt(8.0))
}
Output
2.0

Syntax

Kotlin syntax
kotlin.math.cbrt(volume)
Run these examples in Kotlin

Save one example at a time as Lesson.kt. Use Kotlin 2.2 on JDK 21.

kotlinc Lesson.kt -include-runtime -d lesson.jar
java -jar lesson.jar

Measure a cube-shaped parcel

A parcel's volume is known, and the dispatch team believes the box is a cube. Recover the edge length from that volume before checking the parcel against a shelf opening.

From the programval volume = 125.0
Result5.000
Lesson.kt

fun main() {
    val volume = 125.0

    System.out.printf(java.util.Locale.ROOT, "%.3f%n", (kotlin.math.cbrt(volume)).toDouble())
}
Output
5.000

A parcel holding 125 cubic centimetres has edges of 5 centimetres.

Advertisements

Size a cube-shaped storage bin

The volume of a cube-shaped package is known, but the shelf is labeled by edge length. Recover the length of one edge to see how much horizontal and vertical room the package needs.

From the programval volume = 216.0
Result6.000
Lesson.kt
fun main() {
    val volume = 216.0

    System.out.printf(java.util.Locale.ROOT, "%.3f%n", (kotlin.math.cbrt(volume)).toDouble())
}
Output
6.000

A cube six centimetres on each edge holds 216 cubic centimetres.

Choose the edge of a gift box

A design class models a cube with an internal volume of 64 cubic centimetres. The cutting plan starts with the length of one edge.

GivenVolume 64 cm³
Worked result4 cm

Four identical centimetre units along each dimension give 64 cubic centimetres.

What to watch for

Use consistent units. Cubic centimetres give a result in centimetres.

Documentation for kotlin.math.cbrt