What is Math.cbrt() in Ruby?

The cbrt() function in Ruby returns the cube root of a number.

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

The Math module is required to use this function.

Figure 1: Mathematical representation of the cbrt() function

Syntax

cbrt(number)

Parameter

cbrt() requires a number as a parameter.

Return value

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

Code

The following example shows how to use cbrt() in Ruby.

print "cbrt(8):", Math.cbrt(8), "\n"
print "cbrt(0):", Math.cbrt(0), "\n"
print "cbrt(-25):", Math.cbrt(-25), "\n"
print "cbrt(4.5):", Math.cbrt(4.5), "\n"
New on Educative
Learn to Code
Learn any Language as a beginner
Develop a human edge in an AI powered world and learn to code with AI from our beginner friendly catalog
🏆 Leaderboard
Daily Coding Challenge
Solve a new coding challenge every day and climb the leaderboard

Free Resources