Java Math cbrt()

Overview
Java cbrt() method defined in the Math class calculates the cube root of a double value.
- Java cbrt() method returns NaN if the input argument is NaN.
- Java cbrt() method returns Infinity if the argument is infinite with the same sign as the argument.
- Java cbrt() method returns zero if the argument is zero with the same sign as the argument.
Syntax of Math cbrt() in Java
The syntax of cbrt() method is given below.
The Java cbrt() method is a static method of Math class.
Parameters of Math cbrt() in Java
The cbrt() method takes only single parameter.
- num: A numeric value of double type whose cube root is to be computed.
Return Value of Math cbrt() in Java
- The cbrt() method returns NaN if passed value is NaN.
- The cbrt() method returns zero if number passed is zero with the same sign.
- If the argument is Infinite then cbrt() will return Infinity with same sign.
- Return type is double
Note: If the number passed as argument is a negative number then cbrt() method will return a negative number. cbrt(-num) = -cbrt(num).
Exceptions of Math cbrt() in Java
cbrt() method does NOT throw any exception.
Example
In this sample code, we are going to see how to use cbrt() method in java.
Output
Code Explanation
- In the above code, we have used cbrt() method to find the cube root of 27 and -125.
- As the cube root of 27 is 3 so it will return 3.0.
- And as we discussed above, cube root of a negative number will be negative so cbrt() will return -5.0 as a cube root of -125.
What is Math cbrt() in Java?
The cube root of a numeric value can be computed using the cbrt() method in Java.
More Examples
Example 1
Finding Cube root of Positive Infinity
Output:
Explanation:
- As we discussed above if argument is infinite then it will return Infinity with same sign as argument.
Example 2
In this example we are going to find cube root of zero with positive and negative signs.
Output:
Explanation:
- As discussed above, if the argument is zero, the return value of cbrt() method is also zero with the same sign.
Example 3
In this example we are going see the return value of Java cbrt() method when the argument is NaN.
Output:
Explanation:
- As discussed above, if the argument is NaN, the return value of Java cbrt() method is also NaN.
Conclusion
- Math.cbrt() method is used to find the cube root of given number.
- Java cbrt() method returns NaN if the input argument is NaN.
- Java cbrt() method returns Infinity if the argument is infinite with the same sign as the argument.
- Java cbrt() method returns zero if the argument is zero with the same sign as the argument.