Java cbrt Function

The Java cbrt Function is one of the Java Math Library functions, and it finds the cube root of a specified expression or an individual double value.

Java cbrt Function syntax

The syntax of the Math.cbrt in Java Programming language is as shown below. The following cbrt function accepts a positive double value as an argument. It returns the cube root of the specified expression or Value.

static double cbrt(double number); //Return Type is Double

// In order to use in program: 
Math.cbrt(double number);

Number: It can be a double value or a valid numerical expression to find the cube root.

  • If the number argument is positive or negative double value, Math.cbrt function will return the cube root of a given value.
  • If the number argument is not a number (NaN), java Math.cbrt function will return NaN.
  • Moreover, if it is infinity, Math.cbrt function will return the result as Infinity with the same sign.

Java cbrt Function Example

The Java Math.cbrt Function finds the cube root of a specified expression or an individual double value. In this Java program, We are going to find the cube root and display the output.

// Java Math.cbrt Function
package MathFunctions;

public class CbrtMethod {
	public static void main(String[] args) {
		double a = Math.cbrt(100.90 + 150.10 - 220.50 + 10.50);
		System.out.println("Cube Root results = " + a);	

		System.out.println("\nCube Root of Zero: " + Math.sqrt(0));
		
		System.out.println("\nCube Root of Positive Number: " + Math.cbrt(64));
		System.out.println("Cube Root of Positive Number: " + Math.cbrt(100.95));
		
		System.out.println("\nCube Root of Negative Number: " + Math.cbrt(-850.70));
		System.out.println("Cube Root of Negative Number: " + Math.cbrt(-125.00));
	}
}
Java cbrt Function 1

First, We declared a variable of type Double and performed the Math.cbrt function directly on expression.

double a = Math.cbrt(100.90 + 150.10 - 220.50 + 10.50);
System.out.println("Cube Root results = " + a);

Next, We used the Math.cbrt Function directly on Positive double values.

System.out.println("\nCube Root of Positive Number: " + Math.cbrt(64));
System.out.println("Cube Root of Positive Number: " + Math.cbrt(100.95));

Next, We used the java Math.cbrt Function directly on Negative double values.

System.out.println("\nCube Root of Negative Number: " + Math.cbrt(-850.70));
System.out.println("Cube Root of Negative Number: " + Math.cbrt(-125.00));

Java cbrt on Array example

In this Java program, we find the cube roots of bulk data or array. Here, we are going to declare an array of double type and find the cube root of every element in an array.

package MathFunctions;

public class CbrtOnArrays {
	public static void main(String[] args) {
		
		double [] CbrtArray = {27.00, 64.00, 125.00, -500.90, -124.8799, 1234.8597};

		for (int i = 0; i < CbrtArray.length; i++) {
			System.out.format("Cube Root of Array Element = %.4f\n", Math.cbrt(CbrtArray[i]));
		}
	}
}
Cube Root of Array Element = 3.0000
Cube Root of Array Element = 4.0000
Cube Root of Array Element = 5.0000
Cube Root of Array Element = -7.9418
Cube Root of Array Element = -4.9984
Cube Root of Array Element = 10.7285

Within this Java cbrt function example, We used the Java For Loop to iterate the Array. Within the For Loop, we initialized the i value as 0. Next, compiler will check the condition (i < cbrtArray.length). As along the condition is True statement inside the for loop will be executed. The Java cbrtArray.length finds the length of an array

for (int i = 0; i < CbrtArray.length; i++) {

Here, we used the cbrt Math function directly inside the System.out.format statement. It means the compiler calls the static double cbrt(double number) to find the cube root of corresponding values.

System.out.format("Cube Root of Array Element = %.4f\n", Math.cbrt(CbrtArray[i]));

NOTE: If you want to find the cube root of a single item, then use: Math.cbrt(myArray[index_position])

Java cbrt function on Arraylist example

In this Java program, we are going to declare an ArrayList of double type and find the cube root of list elements.

package MathFunctions;

import java.util.ArrayList;

public class CbrtMethodOnArrayList {
	public static void main(String[] args) {
		
		ArrayList<Double> cbrtList = new ArrayList<Double>(5);
		cbrtList.add(12345.998);
		cbrtList.add(-180.05);
		cbrtList.add(1500.9876);
		cbrtList.add(1897.01);
		cbrtList.add(2596.98);
		cbrtList.add(-9878.1898);
		
		for (double x : cbrtList) {
			System.out.println("Cube Root of ArrayList =  " + Math.cbrt(x));
		}
	}
}
Cube Root of ArrayList =  23.11224153295796
Cube Root of ArrayList =  -5.646738922680259
Cube Root of ArrayList =  11.449654140567208
Cube Root of ArrayList =  12.379122865293457
Cube Root of ArrayList =  13.745362623484676
Cube Root of ArrayList =  -21.45651191561244

In this cbrt function example, we used the For Loop to iterate the double values in ArrayList

for (double x : cbrtList) {

The following statement calls the Math.cbrt method ( static double cbrt(double x) ) to find the cube root of corresponding values.

System.out.println("Cube Root of ArrayList =  " + Math.cbrt(x));