AppDividend
Latest Code Tutorials

# Java Math cbrt() Function Example

Java.lang.Math.cbrt() is an inbuilt method that is used to calculate the cube root of any number (passed as double type). The cbrt() method can come handy in several programs involving mathematical calculations. As defined by Java(Oracle), the computed result for this method must be within 1 ulp(unit of least precision of the exact result.

## Java Math cbrt()

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.

### Syntax

```public static double cbrt(double x)
```

### Parameter(s)

The variable of a double type whose cube root is to be calculated.

### Return Value

The cube root of the value passed.

See the following figure.

#### Note

1. For a positive finite argument x, cbrt(-x) == -cbrt(x)
2. If the argument is NaN, then this method returns NaN.
3. If the argument is negative infinity, then this method returns negative infinity.
4. If the argument is positive infinity, then this method returns positive infinity.
5. If the argument is positive zero or negative zero, then this method returns the same value that is passed.

Consider the following examples.

#### Example1.java: The following example demonstrates the use of this method.

See the following code.

```public class Example1 {
public static void main(String[] args) {
double num = -27.0;
System.out.println(Math.cbrt(num));
}
}
```

#### Output

```Output:
->javac Example1.java
->java Example1
-3.0```

#### Example2.java: The following example demonstrates the situation when the argument is NaN.

```public class Example2 {
public static void main(String[] args) {
System.out.println(Math.cbrt(4.0 % 0));
}
}
```

#### Output

```->javac Example2.java
->java Example2
NaN
```

#### Example3.java: The following example demonstrates the situation of passing positive infinity.

```public class Example3 {
public static void main(String[] args) {
System.out.println(Math.cbrt(Double.POSITIVE_INFINITY));
}
}
```

#### Output

```->javac Example3.java
->java Example3
Infinity```

#### Example4.java: The following example demonstrates the situation of passing negative infinity.

```public class Example4 {
public static void main(String[] args) {
System.out.println(Math.cbrt(Double.NEGATIVE_INFINITY));
}
}
```

#### Output

```->javac Example4.java
->java Example4
-Infinity
```

#### Example5.java: The following example demonstrates the situation of passing negative or positive zero.

```public class Example5 {
public static void main(String[] args) {
System.out.println(Math.cbrt(0.0));
System.out.println(Math.cbrt(-0.0));
}
}
```

#### Output

```->javac Example5.java
->java Example5
0.0
-0.0
```