In Java, Math.cbrt(a) returns the cube root of a: the number that gives a when multiplied by itself three times. Math.cbrt(27) is 3.0.
Use it to go from a volume to an edge length, or to undo a cubed value. It was added in Java 5 (JDK 1.5), and it accepts negative numbers: Math.cbrt(-8) is -2.0.
A static method of the java.lang.Math class, added in Java 5 (JDK 1.5). Call it on the class name, as in Math.cbrt(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass any number; it is widened to double, and the result is a double.
Java
double a = Math.cbrt(27); // 3.0
double b = Math.cbrt(-8); // -2.0
double edge = Math.cbrt(volume); // side of a cube
long r = Math.round(Math.cbrt(n)); // nearest whole root
Call
What it does
Math.cbrt(125)
Returns about 5.0.
Math.cbrt(-27)
Negative input: returns about -3.0.
Math.cbrt(volume)
Edge length of a cube with that volume.
Math.round(Math.cbrt(n))
Nearest whole cube root, for perfect-cube checks.
Java 5 or newer is needed. The result is allowed to be off by one unit in the last place, so round it (or compare with a tolerance) before testing for whole numbers.
Library declarations
Java
// java.lang.Math (Java 5+)
public static double cbrt(double a)
Parameters
Parameter
Description
a
The value whose cube root you want. It may be negative, zero, or positive.
Return Value
Return Value
Description
double
The cube root of a. For negative a the result is negative. NaN gives NaN, infinities stay infinite with the same sign, and zeros keep their sign.
cbrt(-8): -2.0
pow(-8, 1.0 / 3): NaN
cbrt(-0.0): -0.0
cbrt(-Infinity): -Infinity
cbrt(NaN): NaN
How It Works
Odd roots exist below zero. A negative number cubed stays negative, so every negative value has a real cube root, and cbrt finds it.
pow gives up. The exponent 1.0 / 3 is not a whole number, and pow refuses negative bases in that case, returning NaN.
Signs are preserved. Negative zero and negative infinity come back with their minus signs, while NaN in means NaN out.
Example 3 — Check Which Numbers Are Perfect Cubes
Round the cube root to the nearest whole number, cube it again, and compare with the original value.
Java
public class MathCbrtPerfectCube {
public static void main(String[] args) {
long[] values = {27, 28, -64, 1000};
for (long n : values) {
long r = Math.round(Math.cbrt(n));
System.out.println(n + ": nearest root " + r + ", cube? " + (r * r * r == n));
}
}
}
Round, do not cast.Math.round picks the nearest whole root even if cbrt returns something like 2.9999999999999996, where a plain (long) cast would give 2.
Cube it back with integers.r * r * r is exact long arithmetic, so equality with n is a real test: 28 fails because 3 cubed is 27.
Negatives work.cbrt(-64) is about -4.0, the rounded root is -4, and (-4) cubed is -64, so the test passes without any special case for the sign.
Frequently Asked Questions
It returns the cube root of a double. Math.cbrt(27) returns 3.0 and Math.cbrt(-8) returns -2.0.
Java 5 (JDK 1.5). Older versions need Math.pow(x, 1.0 / 3), which fails for negative numbers.
pow does not return a real result for a negative base with a non-whole exponent, and 1.0 / 3 is not whole. cbrt is built for cube roots, so it accepts negative input.
Usually the result is the exact whole number, but Java only promises it is within one unit in the last place. Round with Math.round before comparing to a whole number.
Call Math.cbrt(volume). For a volume of 100 the side is about 4.64.