Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

cbrt
strictmath
java
communitycreator

What is the StrictMath.cbrt method in Java?

Programming Bytes

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

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) );
}
}
Using the StrictMath.cbrt method

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.

RELATED TAGS

cbrt
strictmath
java
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring