Open In App

Java Math.cbrt() Method

Last Updated : 13 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The Math.cbrt() is a part of java.lang.Math package. This method is used to calculate the cube root of a given number. In this article, we are going to discuss how this method works for regular values and for special cases such as infinity and NaN.

Note: A number, when multiplied by itself three times, gives the original number, is known as a cube root of that number. For example, the cube root of 27 is 3 because 3 × 3 × 3 = 27.

Special Cases:

  • If the number is positive, this method will give a positive cube root.
  • If the number is negative, this method will give a negative cube root.
  • If the number is zero, this method will give zero.
  • If the number is infinity, this method will give infinity with the same sign.
  • If the number is NaN, this method will give NaN.

These special cases make sure that the Math.cbrt() methods work correctly.

Syntax of cbrt() Method

public static double cbrt(double a)

  • Parameter: This method takes a single parameter a, of type double. This is the number whose cube root will be calculated.
  • Return Type: This method returns the cube root of the given number a.

Now, we are going to discuss some examples for better understanding.


Examples of Java Math.cbrt() Method

Example 1: In this example, we will see the basic usage of cbrt() method with regular values.

Java
// Java Program to demonstrate
// the working of cbrt() method
import java.lang.Math;

class Geeks {
    
    public static void main(String args[]) {
        
        // Defining different test values
        double a = 125.0;
        // Positive Infinity
        double b = 1.0 / 0; 
        // Negative Infinity
        double c = -(1.0 / 0);  
        // Positive Zero
        double d = 0.0;  
        // Negative Zero
        double e = -0.0;  

        System.out.println(Math.cbrt(a));  
        System.out.println(Math.cbrt(b));
        System.out.println(Math.cbrt(c));  
        System.out.println(Math.cbrt(d));  
        System.out.println(Math.cbrt(e));  
    }
}

Output
5.0
Infinity
-Infinity
0.0
-0.0

Explanation: Here, we are calculating the cube roots of different numbers like the cube root of 125.0 is 5.0 and for positive infinity, the cube root is positive infinity and for negative infinity, the cube root is negative infinity and for both the positive and negative zero the cube root will always be zero.

Example 2: In this example, we will see howcbrt() method handles NaN and Infinity.

Java
// Handling NaN and Infinity
import java.lang.Math;

class Geeks {
    
    public static void main(String args[]) {
        
        // Defining test values for Infinity and NaN
        double p = 1.0 / 0;  
        double n = -(1.0 / 0); 
        double nan = 0.0 / 0.0; 

        System.out.println("Cube root of Positive Infinity: " 
        + Math.cbrt(p));
        System.out.println("Cube root of Negative Infinity: " 
        + Math.cbrt(n));
        System.out.println("Cube root of NaN: " 
        + Math.cbrt(nan));
    }
}

Output
Cube root of Positive Infinity: Infinity
Cube root of Negative Infinity: -Infinity
Cube root of NaN: NaN

Explanation: Here, we are calculating the cube roots of special values like positive infinity, negative infinity and NaN. The cube root of postitive infinity is positive infinity, the cube root of negative infinity is negative infinity and the cube root for NaN is NaN.

Important Points:

  • This method is used when we need to calculate the cube root of any number.
  • This method can work with any type of number like positive, negative and zero, can also work well with infinity and NaN.

Next Article
Practice Tags :

Similar Reads