What is the StrictMath.cbrt method in Java?

The cbrt method is used to find the cube root of a double value. For example, it could find that the cube root of 27 is 3. cbrt is a static method present in the StrictMath class.

Syntax

public static double cbrt(double a)

Argument

This method takes a double value for which the cube root is to be found as an argument.

If the argument is negative, then cbrt(-num) will be computed internally by -cbrt(num).

For NaN , POSITIVE_INFINITY , NEGATTIVE_INFINITY, +0, and -0, the return value is the same as the argument.

Return type

The return type of the cbrt method is double.

Example

The code given below shows us how to use the cbrt method:

class CbrtExample {
public static void main( String args[] ) {
double num = 27.0;
System.out.println( "cbrt(27.0) : " + StrictMath.cbrt(num) );
num = -125.0;
System.out.println( "cbrt(-125.0) : " + StrictMath.cbrt(num) );
num = -0.0;
System.out.println( "cbrt(-0.0) : " + StrictMath.cbrt(num) );
}
}

Explanation

  • Line 3: We create a double variable, num, with a value of 27.0.

  • Line 3: We use the cbrt method to get the cube root value of the num. For the number 27.0, the cube root value will be 3.0. 3*3*3 will produce 27.

  • Lines 6–7: We change the value of the num to -125.0 and call the cbrt method to get the cube root of -125.0. The cube root value will be -5.0, because 5 * 5 * 5 will produce 125. The argument is negative, so the - sign is added to the 5 and -5.0 will be returned.

  • Lines 9–10: We change the value of the num to -0.0 and call the cbrt method to get the cube root of -0.0. The cube root value will be -0.0.