Java Math cbrt()
cbrt() accepts int, float, long or double value as an argument and returns cube root of the argument. The returned value is of type double.
If the argument is NaN, then cbrt() returns NaN.
Following is the syntax of cbrt() method.
double result = cbrt(double value)
If you provide any other numeric type for argument other than double, type promotion happens implicitly.
Example 1 – Math.cbrt(int)
In the following example, we pass a int value as argument to cbrt() method and find the cube root of its value.
Java Program
public class MathExample {
public static void main(String[] args) {
int x = 27;
double a = Math.cbrt(x); //3.0
System.out.println(a);
}
}
Output
3.0
Example 2 – Math.cbrt(float)
In the following example, we pass a float value as argument to cbrt() method and find the cube root of its value.
Java Program
public class MathExample {
public static void main(String[] args) {
float x = 30.25F;
double a = Math.cbrt(x); //3.0
System.out.println(a);
}
}
Output
3.0
Example 3 – Math.cbrt(long)
In the following example, we pass a long value as argument to cbrt() method and find the cube root of its value.
Java Program
public class MathExample {
public static void main(String[] args) {
long x = 30L;
double a = Math.cbrt(x); //3.0
System.out.println(a);
}
}
Output
3.1072325059538586
Example 4 – Math.cbrt(double)
In the following example, we pass a double value as argument to cbrt() method and find the cube root of its value.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = 64D;
double a = Math.cbrt(x); //3.0
System.out.println(a);
}
}
Output
4.0
Example 5 – Math.cbrt(NaN)
In the following example, we pass Double.NaN as argument to cbrt() method. As per the definition of the function, cbrt() should return NaN value.
Java Program
public class MathExample {
public static void main(String[] args) {
double x = Double.NaN;
double a = Math.cbrt(x); //3.0
System.out.println(a);
}
}
Output
NaN
Conclusion
In this Java Tutorial, we learned about Java Math.cbrt() function, with example programs.