简体   繁体   English

递归算法计算平方根和立方根

[英]Recursive algorithm to calculate the square root and cube root

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);  

}

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;
}

You're stopping condition is "if n == num" while n and num are double. 您正在停止条件是“if n == num”而n和num是double。 Double or Float numbers are known to be imprecise, so this condition may never be met. 已知Double或Float数字不精确,因此可能永远不会满足此条件。 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". 当两个数字之间的差异变得“足够小”时,这将停止。

When you are using double you should use delta double value. 当您使用double时,您应该使用delta double值。 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM