使用Leibniz系列查找pi值

时间:2016-01-17 03:39:16

标签: java

我开始学习Java,我的代码有问题。

确定它有明显的错误:它没有运行。我被要求使用Leibniz系列找到pi值,并且还要求迭代次数达到六位有效数字(3.141592)。

到目前为止我有这个:

public class Findingpie2 {
    public static void main(String[] args) {
        double pi = 0.0;
        int counter = 1;
        for (int n = 0; n < counter; n++) {
            pi += Math.pow(-1, n) / (2*n + 1);
            counter++;
            if (pi==3.141592) {
                System.out.println("the value of pi is: "+String.format("%6f",4*pi));
                System.out.println("the number of iterations for pi value is "+n);
            }
        }
    }
}

2 个答案:

答案 0 :(得分:1)

仅使用容差标准,显示结果而不进行任何舍入:

package dummy;

import static java.lang.String.format;
import static java.lang.System.out;

import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Map.Entry;

/*
 * Finding pi value using Leibniz series
 * 
 * The Leibniz series is converging. To compare two successive values
 * is enough to get the required precision.
 * 
 * http://stackoverflow.com/questions/34834854/finding-pi-value-using-leibniz-serie
 * 
 */
public class Findingpie2 {

    public static void main(String[] args) {
        out.println("Pi, no rounding:");
        for (int i = 2; i < 10; i++) {
            double tolerance = Math.pow(0.1, i);
            Entry<Integer, Double> result = calcpi(tolerance);
            String pi = result.getValue().toString().substring(0,  i+1);
            out.println(format("The value of pi is: %s with %." + i + "f tolerance (%d iterations)." , pi, tolerance, result.getKey()));
        }
    }

    private static Entry<Integer, Double> calcpi(double tolerance) {
        int n = 0;
        double pi = 0;
        double bpi = 10 * tolerance;
        double inc = 1;
        while (Math.abs(bpi - pi) > tolerance) {
            bpi = pi;
            pi += inc / (2*n + 1);
            inc = -inc;
            n++;
        }
        return new SimpleImmutableEntry<Integer, Double>(n, 4 * pi);
    }

}

更新:它会显示:

Pi, no rounding:
The value of pi is: 3.1 with 0,01 tolerance (51 iterations).
The value of pi is: 3.14 with 0,001 tolerance (501 iterations).
The value of pi is: 3.141 with 0,0001 tolerance (5001 iterations).
The value of pi is: 3.1416 with 0,00001 tolerance (50001 iterations).
The value of pi is: 3.14159 with 0,000001 tolerance (500001 iterations).
The value of pi is: 3.141592 with 0,0000001 tolerance (5000001 iterations).
The value of pi is: 3.1415926 with 0,00000001 tolerance (50000001 iterations).
The value of pi is: 3.14159265 with 0,000000001 tolerance (499999987 iterations).

答案 1 :(得分:0)

请改用:

public static void main(String[] args) {

          double pi = 0.0;
           int MAX_ITERATIONS=1000;
            int n=0;
            while(true) {
                pi += Math.pow(-1, n) / (2*n + 1);
                n++;
                if (n>MAX_ITERATIONS) {
                    System.out.println("the value of pi is: "+String.valueOf(4*pi));
                    System.out.println("the number of iterations for pi value is "+n);
                    break;
                }
            }
    }

结果是:
pi的值是: 3.1425916543395442
pi值的迭代次数 1001

现在,如果您想达到精确的精确度,请执行以下操作:
定义一个可接受的错误,在您的情况下,您需要是3.141592。因此,您需要容忍的误差小于0.0000001。更改上面的代码如下:

    public static void main(String[] args) {
    double pi = 0.0;
    double ERROR = 0.0000001;
    int n=0;
    while(true) {
        pi += Math.pow(-1, n) / (2*n + 1);
        n++;
        if (Math.abs(4*pi-Math.PI)<ERROR) {
                    System.out.println("the value of pi is:        "+String.valueOf(4*pi));
                    System.out.println("the number of iterations for pi value is "+n);
                    break;
                }
            }
    }

结果是:

the value of pi is: 3.1415919000000936
the number of iterations for pi value is 1326982