Scala Tutorial Scala References

Scala - Math cbrt() Method



The Scala Math cbrt() method returns the cube root of the given number. In special cases it returns the following:

  • If the argument is NaN, then the result is NaN.
  • If the argument is infinite, then the result is an infinity with the same sign as the argument.
  • If the argument is zero, then the result is a zero.

Syntax

def cbrt(x: Double): Double = java.lang.Math.cbrt(x)

Parameters

x Specify a number.

Return Value

Returns the cube root of the specified number.

Exception

NA.

Example:

In the example below, cbrt() method is used to find out the cube root of the given number.

import scala.math._

object MainObject {
  def main(args: Array[String]) {
    println(s"cbrt(10) = ${cbrt(10)}");  
    println(s"cbrt(100) = ${cbrt(100)}"); 
    println(s"cbrt(50) = ${cbrt(50)}"); 
    println(s"cbrt(0) = ${cbrt(0)}");
    println(s"cbrt(-50) = ${cbrt(-50)}");
    println(s"cbrt(Double.NaN) = ${cbrt(Double.NaN)}"); 
    println("cbrt(Double.PositiveInfinity) = "
            + cbrt(Double.PositiveInfinity));
    println("cbrt(Double.NegativeInfinity) = "
            + cbrt(Double.NegativeInfinity));            
  }
}

The output of the above code will be:

cbrt(10) = 2.154434690031884
cbrt(100) = 4.641588833612779
cbrt(50) = 3.6840314986403864
cbrt(0) = 0.0
cbrt(-50) = -3.6840314986403864
cbrt(Double.NaN) = NaN
cbrt(Double.PositiveInfinity) = Infinity
cbrt(Double.NegativeInfinity) = -Infinity

❮ Scala - Math Methods