Java – Find Cube Root – cbrt() Method – Examples & Explanation

java.lang.Math.cbrt() method is used to find the cube root of a double value in JAVA for the given input (x – parameter).
For positive finite x, cbrt(-x) == -cbrt(x); that is, the cube root of a negative value is the negative of the cube root of that value’s magnitude.
The computed result must be within 1 ulp of the exact result.

cbrt() method exists in Math class of java.lang package.
Since: JDK1.5

 

Declaration of cbrt() Method

The declaration of cbrt() method is:

public static double cbrt(double x)

 

Syntax of cbrt() Method

The syntax of cbrt() method in Java is:

 
java.lang.Math.cbrt(double x)

Example

/*
*Find the cube root of given input(double) without importing the 
java.lang Package.
*/
public class JavaMathExample {
    public static void main(String[] args) {
        //Declaring variables
       double x = 27;
        double result;
        //Assign cube root of x to result variable
        result = java.lang.Math.cbrt(x);
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

3.0

Example
By importing java.lang package the syntax of cbrt() method can be used as:

/*
*Find the cube root of given input(double) by importing the java.lang Package.
*/
import java.lang.*;  //importing package

public class JavaMathExample {
    public static void main(String[] args) {
        
        //Declaring variables
        double x = -27;
        double result;
        
        //Assign cube root of x to result variable
        result = Math.cbrt(x);
        
        //Printing result to the console
        System.out.println(result);
    }
}

Output:

-0.3

 

 

Parameters of cbrt() Method in Java

x – Where x is any valid double input value. This parameter is required.

 

Error Handling

If the x parameter is not a number cbrt() method returns an error.
If there is no argument (x – input value) passes to the method, the compiler will produce an error.Example

/*
*This example returns a compile time error
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
        String x = "Not a number";
        double result;

        //argument is not a number
        result = Math.cbrt(x);
        System.out.println(result);

        //Here no argument is used
        result = Math.cbrt();
        System.out.println(result);


    }

}

Output:
Error

 

Return Value of cbrt() Method in Java

cbrt() method returns the cube root of a double value for the given input (x – parameter).

 

Java cbrt() Method Example 1

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        double negativeValue = -18.869;
        double positiveValue = 91.0;
        double zero = 0;
        System.out.println("cube root of a Negative Value: "+Math.cbrt(negativeValue));
        System.out.println("cube root of a Positive Value: "+Math.cbrt(positiveValue));
        System.out.println("cube root of Zero: "+Math.cbrt(zero));

    }
}

Output:

cube root of a Negative Value: -2.6622548577813987
cube root of a Positive Value: 4.497941445275415
cube root of Zero: 0.0

 

Java cbrt() Method Example 2

import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {
        //Declare and initialize an array containing double values
        double test [] = { Double.MAX_VALUE, Double.MIN_VALUE, 
		Double.NaN, Double.MIN_EXPONENT, 42.867e-12, 16.852, 0, -2.58, -78.8569e7 };

        //Use for loop to print cube root of each element of the array
        for (int i=0; i < test.length; i++) {
            System.out.println(  Math.cbrt(test[i])  );
        }

    }
}

Output:

5.643803094122362E102
1.7031839360032603E-108
NaN
-10.072802033522548
3.499782299384434E-4
2.5637980732444143
0.0
-1.3715339700741747
-923.8750385410053

 

Special Cases of cbrt() Method in Java

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 with the same sign as the argument.
Example

/*
*This example shows some special cases of cbrt() method in Java
*/
import java.lang.*; //importing java.lang package

public class JavaMathExample {
    public static void main(String[] args) {

        //Declaring variables
        double positiveZero = 0;
        double negativeZero = -0;
        double positiveInfinity = Double.POSITIVE_INFINITY;
        double negativeInfinity = Double.NEGATIVE_INFINITY;
        double nan = Double.NaN;
        double result;

        //Here argument is positive zero, output will also be positive zero
        result = Math.cbrt(positiveZero);
        System.out.println(result);

        //Here argument is negative zero, output will be positive zero
        result = Math.cbrt(negativeZero);
        System.out.println(result);

        //Here argument is positive infinity, output will also be positive infinity
        result = Math.cbrt(positiveInfinity);
        System.out.println(result);

        //Here argument is negative infinity, output will be negative infinity
        result = Math.cbrt(negativeInfinity);
        System.out.println(result);

        //Here argument is NaN, output will be NaN
        result = Math.cbrt(nan);
        System.out.println(result);


    }

}

Output:

0.0
0.0
Infinity
-Infinity
NaN