4

when I run my code works sometimes but other me I get this error:

Exception in thread "main" java.lang.StackOverflowError       
at squareroot.SquareRoot.GetSquareRoot (SquareRoot.java: 9)   
at squareroot.SquareRoot.GetSquareRoot (SquareRoot.java: 13)   
at squareroot.SquareRoot.GetSquareRoot (SquareRoot.java: 13)`

I was checking my code and I do not enter an infinite loop, please how do I fix this problem?, Thanks.

public static double GetSquareRoot(double n, double low, double high) {
    double sqrt = (low + high) / 2;
    if (sqrt*sqrt > n)
        return GetSquareRoot(n, low, sqrt);
    if (sqrt*sqrt < n)
        return GetSquareRoot(n, sqrt, high);
    return sqrt;
}
public static double Sqrt(double n){
    return GetSquareRoot(n, 0, n);
}

public static double GetCubicRoot(double n, double low, double high) {
    double cbrt = (low + high) / 2;
    if (cbrt*cbrt*cbrt > n)
        return GetCubicRoot(n, low, cbrt);
    if (cbrt*cbrt*cbrt < n)
        return GetCubicRoot(n, cbrt, high);
    return cbrt;
}
public static double Cbrt(double n) {
    return GetCubicRoot(n, 0, n);
}

public static void main(String[] args) {
    Scanner Input = new Scanner(System.in);

    double n = Input.nextDouble();
    double sqrt = Sqrt(n);
    double cbrt = Cbrt(n);

    System.out.println("Raiz cuadrada igual a: "+ sqrt);        
    System.out.println("Raiz cubica igual a: "+ cbrt);  

}
1
  • 1
    You might want to approximate the comparison of sqrt*sqrt == n because floating point comparisons arent exact. Commented Feb 28, 2013 at 22:52

3 Answers 3

10

Your results are not ever likely hitting the end condition because multiplying the numbers is not likely to produce the exact number, you have to introduce a margin of error because square roots aren't usually exact and floating arithmetic uses approximations due to floating point limitations.

public static double GetSquareRoot(double n, double low, double high) {
    double errorMargin = 0.001;        
    double sqrt = (low + high) / 2;
    double diff = sqrt*sqrt - n;
    if ( diff > errorMargin)
        return GetSquareRoot(n, low, sqrt);
    if ( -diff > errorMargin)
        return GetSquareRoot(n, sqrt, high);
    return sqrt;
}
Sign up to request clarification or add additional context in comments.

Comments

6

You're stopping condition is "if n == num" while n and num are double. Double or Float numbers are known to be imprecise, so this condition may never be met. Instead use this

if(Math.abs(sqrt*sqrt - n) < .001)
     return sqrt;

This will stop when the difference between the two numbers gets "small enough".

Comments

5

When you are using double you should use delta double value. Because your calculated value might not be equal as you expect.

bool CompareDoubles2 (double A, double B) 
{
   diff = A - B;
   return (diff < EPSILON) && (-diff > EPSILON);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.