Java cbrt() method with Examples


The java.lang.Math.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. Special cases −

  • If the argument is NaN, then the result is NaN.

  • If the argument is infinite, then the result is an infinity with the same sign as the argument.

  • If the argument is zero, then the result is a zero with the same sign as the argument.

Example

Following is an example to implement the cbrt() method in Java −

import java.lang.*;
public class Example {
   public static void main(String[] args) {
      // get two double numbers
      double x = 125;
      double y = 10;
      // print the cube roots of three numbers
      System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
      System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));
      System.out.println("Math.cbrt(-27)=" + Math.cbrt(-27));
   }
}

Output

Math.cbrt(125.0)=5.0
Math.cbrt(10.0)=2.154434690031884
Math.cbrt(-27)=-3.0

Example

Let us see another example −

import java.lang.*;
public class Example {
   public static void main(String[] args) {
      // get two double numbers
      double x = 0.0;
      double y = -343.0;
      // print the cube roots of three numbers
      System.out.println("Math.cbrt(" + x + ")=" + Math.cbrt(x));
      System.out.println("Math.cbrt(" + y + ")=" + Math.cbrt(y));
   }
}

Output

Math.cbrt(0.0)=0.0
Math.cbrt(-343.0)=-7.0

Updated on: 24-Sep-2019

91 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements