Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

scala
math
cuberoot
communitycreator

What is Math.cbrt() in Scala?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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)}");
}

RELATED TAGS

scala
math
cuberoot
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring