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));
}
}
Output5.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));
}
}
OutputCube 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.
Similar Reads
StrictMath cbrt() Method in Java The java.lang.StrictMath.cbrt() is an inbuilt method in Java which is used to return the cube root of a given double value. The method shows three special results: The result is a zero with the same sign as the argument when the given argument is zero.The result is infinity with the same sign of arg
2 min read
Method Class | isBridge() Method in Java The java.lang.reflect.Method.isBridge() method is used to check whether a function is a Bridge Function or not. This method returns true if method object is a bridge method otherwise it returns false. Bridge Method: These are methods that create an intermediate layer between the source and the targe
3 min read
java.math class and its methods | Set 1 Math class provides mathematical functions to perform basic numeric operations such as exponential, logarithm, square root, and trigonometric functions. cosh, sin, tan, abs, bitLength, multiply and many more. Implementations of the functions of Math class do not return bit-for-bit same results. Henc
4 min read
java.math class and its methods | Set 3 java.math class and its methods | Set 1 java.math class and its methods | Set 2 ceil() : java.math.ceil(double a) method returns the smallest possible value which is either greater or equal to the argument passed. The returned value is a mathematical integer.Special Case : Result is same, if the ret
5 min read
Java cbrt() method with Examples 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 time
3 min read