The error is that this line:

return ob.GetSquareRoot(n, 0, n);

(which is, of course, misnamed) tries to find a solution between 0 and n. However, if 0 < n < 1, then n1/3 > n, so you will never find a solution in the interval (0, n). You'll need to make a special case of this; for example, if 0 < n < 1, you can search the interval (n, 1) instead.

Also, using 0 and n as the bounds won't work for negative numbers, so if you want to make your method more complete and handle those cases, you'll need special cases for those too (probably different for -1 < n < 0 and n < -1).

MORE: After seeing your comment about StackOverflowError, it's occurred to me that you have an additional problem: that you're expecting an exact result. When you put in 0.008, the cube root is 0.2. However, neither 0.008 nor 0.2 can be represented exactly as a floating-point number. The consequence is that if you let cbrt = whatever value is closest to 0.2 that can be represented, then cbrt*cbrt*cbrt won't be exactly 0.008. It can't be exactly 0.008 anyway, since 0.008 can't be represented as a double; however, if n is whatever value is closest to 0.008, then it's likely that cbrt*cbrt*cbrt will not be exactly equal to n, due to roundoff errors. For a calculation like this, it's important that you not compare doubles for equality; instead, compare them using an "epsilon" value, or a margin of error. Thus, after

double cbrt = (low + high) / 2;

you should have something like

if (Math.abs(cbrt*cbrt*cbrt - n) < EPSILON)
    return cbrt;

where EPSILON is some small value such as 1E-10. (I've sometimes seen code where EPSILON is computed to be a relative value, i.e. abs(n * RELATIVE_EPSILON), instead of an absolute value.)

Another way to avoid StackOverflowError is to quit when low and high become really close, because by that point you're not going to gain much more accuracy, and you need to make sure you exit the algorithm even if the value of cbrt*cbrt*cbrt is a little bit off. Something like

if (high - low < EPSILON)
    return cbrt;

See also What's wrong with using == to compare floats in Java?.

Answer from ajb on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ java โ€บ ref_math_cbrt.asp
Java Math cbrt() Method
System.out.println(Math.cbrt(0)); System.out.println(Math.cbrt(1)); System.out.println(Math.cbrt(27)); System.out.println(Math.cbrt(0.64)); System.out.println(Math.cbrt(-27)); ... The cbrt() method returns the cube root of a number.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-program-to-find-the-cube-root-of-a-given-number-using-binary-search
Java Program to Find the Cube Root of a Given Number Using Binary Search - GeeksforGeeks
July 23, 2025 - Input: x = 120 Output: 4 Explanation: The cube root of 120 lies in between 4 and 5 so floor of the cube root is 4. ... Check the cube of every element till n and store the answer till the cube is smaller or equal to the n ... // Java Program to Find the cube root // of given number using Naive approach import java.io.*; class GFG { static int cuberoot(int n) { int ans = 0; for (int i = 1; i <= n; ++i) { // checking every number cube if (i * i * i <= n) { ans = i; } } return ans; } public static void main(String[] args) { // Number int number = 27; // Checking number int cuberoot = cuberoot(number); System.out.println(cuberoot); } }
๐ŸŒ
Codecademy
codecademy.com โ€บ docs โ€บ java โ€บ math methods โ€บ .cbrt()
Java | Math Methods | .cbrt() | Codecademy
November 9, 2022 - The .cbrt() method will return the cube root of num. The value of num can be any real number. Edge cases for this method include: If the argument is NaN, the return value will be NaN.
๐ŸŒ
Programiz
programiz.com โ€บ java-programming โ€บ library โ€บ math โ€บ cbrt
Java Math cbrt()
class Main { public static void main(String[] args) { // create a double variable double value1 = Double.POSITIVE_INFINITY; double value2 = 27.0; double value3 = -64; double value4 = 0.0; // cube root of infinity System.out.println(Math.cbrt(value1)); // Infinity // cube root of a positive number System.out.println(Math.cbrt(value2)); // 3.0 // cube root of a negative number System.out.println(Math.cbrt(value3)); // -4.0 // cube root of zero System.out.println(Math.cbrt(value4)); // 0.0 } }
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ java โ€บ java-math-cbrt-method
Java Math.cbrt() Method - GeeksforGeeks
May 13, 2025 - This method is used when we need to calculate the cube root of any number. This method can work with any type of number like positive, negative and zero, can also work well with infinity and NaN.
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ java โ€บ lang โ€บ math_cbrt.htm
Java - Math cbrt(double) Method
The Java 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.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ standard-library โ€บ java โ€บ lang โ€บ Math โ€บ cbrt
Java Math cbrt() - Compute Cube Root | Vultr Docs
November 28, 2024 - In Java, the Math.cbrt() method is a straightforward and efficient way to calculate the cube root of a number.
Top answer
1 of 2
2

The error is that this line:

return ob.GetSquareRoot(n, 0, n);

(which is, of course, misnamed) tries to find a solution between 0 and n. However, if 0 < n < 1, then n1/3 > n, so you will never find a solution in the interval (0, n). You'll need to make a special case of this; for example, if 0 < n < 1, you can search the interval (n, 1) instead.

Also, using 0 and n as the bounds won't work for negative numbers, so if you want to make your method more complete and handle those cases, you'll need special cases for those too (probably different for -1 < n < 0 and n < -1).

MORE: After seeing your comment about StackOverflowError, it's occurred to me that you have an additional problem: that you're expecting an exact result. When you put in 0.008, the cube root is 0.2. However, neither 0.008 nor 0.2 can be represented exactly as a floating-point number. The consequence is that if you let cbrt = whatever value is closest to 0.2 that can be represented, then cbrt*cbrt*cbrt won't be exactly 0.008. It can't be exactly 0.008 anyway, since 0.008 can't be represented as a double; however, if n is whatever value is closest to 0.008, then it's likely that cbrt*cbrt*cbrt will not be exactly equal to n, due to roundoff errors. For a calculation like this, it's important that you not compare doubles for equality; instead, compare them using an "epsilon" value, or a margin of error. Thus, after

double cbrt = (low + high) / 2;

you should have something like

if (Math.abs(cbrt*cbrt*cbrt - n) < EPSILON)
    return cbrt;

where EPSILON is some small value such as 1E-10. (I've sometimes seen code where EPSILON is computed to be a relative value, i.e. abs(n * RELATIVE_EPSILON), instead of an absolute value.)

Another way to avoid StackOverflowError is to quit when low and high become really close, because by that point you're not going to gain much more accuracy, and you need to make sure you exit the algorithm even if the value of cbrt*cbrt*cbrt is a little bit off. Something like

if (high - low < EPSILON)
    return cbrt;

See also What's wrong with using == to compare floats in Java?.

2 of 2
-1
//How about My Solution - Without Recursive

import java.util.Scanner;

public class CubeRoot {

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String str = input.next();
    Boolean negative = false;
    if (str.startsWith("-")) {
        str = str.substring(1);
        negative = true;
    }
    if (str.indexOf(".") > 0) {
        // find the poisition of '.'
        int pPos = str.length() - 1 - str.indexOf(".");
        String plainStr = (str.substring(0, str.indexOf('.')).concat(str.substring(str.indexOf('.') + 1)));
        StringBuffer cStr = new StringBuffer(cubeRoot(plainStr));

        if (cStr.toString().equalsIgnoreCase(plainStr))
            System.out.println("couldn't compute Cube Root for this :(");
        else {
            if (cStr.length() > pPos / 3) // devide 3 times to put the '.'
                cStr.insert(cStr.length() - pPos / 3, ".");
            else {
                int cStrLength = cStr.length();
                for (int i = 0; i <= pPos / 3 - cStrLength; i++)
                    cStr = new StringBuffer("0").append(cStr.toString());
                cStr.insert(cStr.length() - pPos / 3, ".");
            }
            String append = (negative) ? new String("-") : new String("");
            System.out.println(append + cStr.toString());
        }
    } else {
        System.out.println("Not a floating num");
    }
}

private static String cubeRoot(String d) {
    Long l = new Long(d);
    for (int i = 0; i < l; i++) {
        if (i * i * i == l) {
            return String.valueOf(i);
        }
    }
    return d;

}

}
Find elsewhere
๐ŸŒ
AlphaCodingSkills
alphacodingskills.com โ€บ java โ€บ pages โ€บ java-program-cube-root-of-a-number.php
Java Program Cube Root of a Number - AlphaCodingSkills
March 3, 2021 - If a number is multiplied by itself ... the number. If x is the cube root of y, it can be expressed as below: ... The cbrt() method of Java Math class can be used to return cube root of a number....
๐ŸŒ
Programzools
programzools.com โ€บ find-squareroot-and-cuberoot-in-java.html
Find square root and cube root in Java - ProgramZools
March 23, 2025 - Find square root and cube root in Java - Well organized and easy to understand programming tutorials with lots of examples of why,what and how to program in Java and OOPs
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-the-mathcbrt-method-in-java
What is the Math.cbrt() method in Java?
The cube root of the number 27 is 3. We can see how in the example below. ... We use the Math.cbrt() method in Java to find the cube root of a given number.
๐ŸŒ
YouTube
youtube.com โ€บ watch
How to calculate Square root and Cube root of a number in Java? - YouTube
โ–บ โ–บPersonal queries? - Follow me on LinkedIn - https://www.linkedin.com/in/dinesh-varyani/ โ–บ http://www.hubberspot.com
Published ย  September 1, 2022
๐ŸŒ
TutorialsPoint
tutorialspoint.com โ€บ java-program-to-find-cube-root-of-a-number-using-binary-search
Java program to find cube root of a number using binary search
Following are the steps to find the cube root of a number using the binary search algorithm ? Consider a number n and initialize low=0 and right=n (given number). Find the mid value of low and high using mid = low + (high-low)/2. find the value of mid * mid*mid, if mid * mid*mid == n then return the mid value. If the mid value is less than n then low=mid+1 else high =mid-1 ยท Repeat steps 2 to 4 until we find the value. Below is the Java program to find the cube root of a number using the binary search algorithm ?
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ what-is-mathcbrt-in-java
What is Math.cbrt in Java?
1 week ago - The cbrt method returns the cube root of the argument. For example, the cube root of 27 is 3. The return type of the cbrt method is double.
๐ŸŒ
Kodejava
kodejava.org โ€บ how-do-i-calculate-cube-root-and-square-root-of-a-number
How do I calculate cube root and square root of a number? - Learn Java by Examples
4 weeks ago - package org.kodejava.math; public class CubeSquareRootExample { public static void main(String[] args) { double cube = 125.0d; double square = 100.0d; // Get the cube root of double value double cubeRoot = Math.cbrt(cube); System.out.println("Cube root of " + cube + " is " + cubeRoot); // Get the square root of double value double squareRoot = Math.sqrt(square); System.out.println("Square root of " + square + " is " + squareRoot); } }
๐ŸŒ
Chalmers University
cse.chalmers.se โ€บ edu โ€บ year โ€บ 2018 โ€บ course โ€บ TDA352 โ€บ progass โ€บ setup โ€บ java โ€บ CubeRoot.java
CubeRoot.java
This class provides the method ... Functions" * Available at: http://arxiv.org/abs/0908.3030 */ public class CubeRoot { /** * Returns the cube root for big integers....
๐ŸŒ
Tutorial Gateway
tutorialgateway.org โ€บ java-cbrt-function
Java cbrt Function
May 31, 2022 - The Java cbrt Function is one of the Math Library functions, and it finds the cube root of a specified expression or an individual double value.
๐ŸŒ
Tutorjoes
tutorjoes.in โ€บ Java_example_programs โ€บ find_the_cube_root_of_a_number_using_library_method_in_java
Write a Java program to Find the cube root of a number using library method
May 4, 2023 - Scanner input = new Scanner(System.in); : This line creates a new ... System.out.print("Enter The Number : ");: This line prints a message asking the user to enter a number. ... Math.cbrt() method to find the cube root of the number entered by the user, and then prints the result to the console.
๐ŸŒ
Educative
educative.io โ€บ answers โ€บ how-to-get-the-square-cube-and-square-root-of-a-number-in-java
How to get the square, cube, and square root of a number in Java
In Java, we can get the square, cube, and square root of a number using the Math class. With the Math.pow() method, we can get the cube and square of the number.