Close

Java - Math.cbrt() Examples

Java Java API 


Class:

java.lang.Math

java.lang.Objectjava.lang.Objectjava.lang.Mathjava.lang.MathLogicBig

Method:

public static double cbrt(double a)

Returns the cube root of a double value. 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.


Examples


package com.logicbig.example.math;

public class CbrtExample {

public static void main(String... args) {
findCubeRoot(216);
findCubeRoot(10);
findCubeRoot(-27);
findCubeRoot(0);
}

private static void findCubeRoot(double value) {
String sign = value > 0 ? "Positive" : value < 0 ? "Negative" : "Zero";

double CubeRoot = Math.cbrt(value);
System.out.printf("The CubeRoot of %s Number %s = %.2f %n", sign, value, CubeRoot);
}

}

Output

The CubeRoot of Positive Number 216.0 = 6.00 
The CubeRoot of Positive Number 10.0 = 2.15
The CubeRoot of Negative Number -27.0 = -3.00
The CubeRoot of Zero Number 0.0 = 0.00




package com.logicbig.example.math;

public class CbrtExample2 {

public static void main(String... args) {
findCubeRoot(729);
findCubeRoot(100);
findCubeRoot(-64);
}

private static void findCubeRoot(double value) {
double cubeRoot = Math.cbrt(value);
double cube = cubeRoot * cubeRoot * cubeRoot;
String perfect = cube == value ? "Perfect Cube" : "Not Perfect Cube";

System.out.printf("The CubeRoot of Number %s = %.2f (%s)%n", value, cubeRoot, perfect);
}

}

Output

The CubeRoot of Number 729.0 = 9.00 (Perfect Cube)
The CubeRoot of Number 100.0 = 4.64 (Not Perfect Cube)
The CubeRoot of Number -64.0 = -4.00 (Perfect Cube)




See Also