简体   繁体   English

堆栈溢出错误(Java)

[英]Stack Overflow Error (Java)

So my code is printing out estimations of Pi using a for and while loop and a recursive method. 所以我的代码使用for和while循环以及递归方法打印出对Pi的估计。 It's all working except my compiler is saying there's a stack overflow error for the if statement in my recursive method. 一切正常,除了我的编译器说我的递归方法中的if语句有一个堆栈溢出错误。

public static final double REAL_PI = 3.14159;//PI is the value Mr.B gave us on the handout
public static double Pi = 0; //Pi is the value of Pi that this program calculates
public static int m = 0; 
public static int c = 0;


public static void main (String [] args)
{
    Algorithm(); //calls on method of calculating pi
    System.out.println("Calculated pi: " + Pi); //prints out pi
    countDigits(Pi); //calls on countdigits method
    System.out.println("Number of digits: " + c); //has the computer print out the count because that's how many digits are the same
    While();
    Recursive(1, 0.0); //calls on estimate digits method
}

public static double Algorithm() //should return a double (pi)
{
    for(m=1; m<=100000; m++)
    {
        Pi += 4*(Math.pow(-1, m-1)/((2*m)-1));//Math.pow uses math package to calculate a power to use the algorithm
    }
    return Pi;
}

public static int countDigits (double Pi)
{
    int a = (int) Pi; //the int cast makes Pi and REAL_PI into integers so the program can compare each digit separately 
    int b = (int) REAL_PI;
    int c = 0;
    int count = 0;
    while(a == b)//if m less then or equal to 100,000 then while loop runs
    {
        count ++;
        a = (int) (Pi*(Math.pow(10,count))); //if a=b then the computer will multiply Pi and REAL_PI by 10 
        b = (int) (REAL_PI*(Math.pow(10,count)));
        /*when you input a and b 
         * while loop compares them
         * if a = b then loop continues until a doesn't equal b and loop ends
         */
    }
    c = count; //gives c the value of the count so it can be used outside the method
    return count;
}
public static double While()
{
    int m = 1;
    Pi = 0.0;
    while (countDigits(Pi) < 6) 
    {
        Pi += 4*(Math.pow(-1, m-1)/((2*m)-1));
        m++;
    }
    Pi = (int)(Pi * 1000000);
    Pi = (double)(Pi/1000000);
    System.out.println("Pi using while loop: " + Pi);
    return Pi;
}

public static double Recursive(int m,double Pi)
{
    Pi += 4*(Math.pow(-1, m-1)/((2*m)-1));
    if (countDigits(Pi) < 6)
    {
        return Pi += Recursive(m+1,Pi);
    }
    Pi = (int)(Pi * 1000000);
    Pi = (double)(Pi/1000000);
    System.out.println("Pi using recursive: " + Pi);
    return Pi;
}

} }

The problem is that the Leibniz series for computing π converges EXTREMELY slowly. 问题是,莱布尼兹系列计算π收敛极为缓慢。 Using your program, I found that after 3663 iterations (when I killed the program), the values looked like this: 使用您的程序,我发现在3663次迭代后(杀死程序时),值看起来像这样:

pi=3.141865802997432 
pi=3.1413195787723875 
pi=3.1418656538577117 
pi=3.1413197278306884 

Still only 3 decimal places, and it is going to take a long time even to be accurate to 4. The stack is not big enough to hold so many recursive calls, and eventually it will overflow. 仍然只有3位小数,要精确到4位还需要花费很长时间。堆栈不足以容纳这么多的递归调用,最终将溢出。

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

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