What is Math.cbrt() in Scala?

The cbrt() function returns the cube root of a number sent as a parameter.

Figure 1 below shows the mathematical representation of the cbrt() function.

Figure 1: Mathematical representation of the cbrt() function

The following module is required for this function.

import scala.math._

Syntax

Double cbrt(Double number)

Parameter

This function requires a number as a parameter.

Return value

cbrt() returns the cube root of a number sent as a parameter.

  • If the parameter value is PositiveInfinity, then it returns positive infinity.
  • If the parameter value is NegativeInfinity, then it returns negative infinity.
  • If the parameter value is NaN, then it returns NaN.

Example

import scala.math._
object Main extends App {
//positive number
println(s"The value of cbrt(10) = ${cbrt(10)}");
println(s"The value of cbrt(0) = ${cbrt(0)}");
println(s"The value of cbrt(9) = ${cbrt(9)}");
//negative number
println(s"The value of cbrt(-9) = ${cbrt(-9)}");
//error outputs
println(s"The value of cbrt(Double.PositiveInfinity) = ${cbrt(Double.PositiveInfinity)}");
println(s"The value of cbrt(Double.NegativeInfinity) = ${cbrt(Double.NegativeInfinity)}");
println(s"The value of cbrt(Double.NaN) = ${cbrt(Double.NaN)}");
}

Free Resources