What is Math.cbrt in Java?

The cbrt method is used to find the cube root of a double value. cbrt is a static method present in the Math class.

Syntax

Math.cbrt(double num);

The cbrt method returns the cube root of the argument. For example, the cube root of 27 is 3. The return type of the cbrt method is double. If the argument is negative, then the cbrt(-num) will be computed internally by -cbrt(num).

For NaN , POSITIVE_INFINITY , NEGATTIVE_INFINITY, +0, -0 the return value is the same as the argument.

Example

class CbrtExample {
public static void main( String args[] ) {
double num = 27.0;
System.out.println( "cbrt(27.0) : " + Math.cbrt(num) );
num = -125.0;
System.out.println( "cbrt(-125.0) : " + Math.cbrt(num) );
num = 1.0; // infinity
System.out.println( "cbrt(1.0) : " + Math.cbrt(num) );
num = -0.0;
System.out.println( "cbrt(-0.0) : " + Math.cbrt(num) );
num = 10; // infinity
System.out.println( "cbrt(10) : " + Math.cbrt(num) );
}
}

In the code above, we have used the Math.cbrt function to find the cube root of the numbers.

  • For the number 27.0, the cube root value will be 3.0. 3*3*3 will produce 27.

  • For the number -125.0, the cube root value will be -5.0. 5 * 5 * 5 will produce 125. The argument is negative so the - sign is added to the 5 and -5.0 will be returned.

  • For the number 1.0, the cube root value will be 1.0.

  • For the number -0.0, the cube root value will be -0.0.

  • For the number 10, the cube root value will be 2.154434690031884.

Free Resources