简体   繁体   English

使用Leibniz系列求pi值

[英]Finding pi value using Leibniz series

I am starting to learn Java and I have a problem with my code. 我开始学习Java,我的代码有问题。

For sure it has obvious mistakes: it doesn't run. 当然,它有明显的错误:它不会运行。 I was asked to find the pi value using Leibniz series and also the number of iterations to reach six significant digit (3.141592). 有人要求我使用莱布尼兹(Leibniz)系列找到pi值,还要找到达到六个有效数字(3.141592)的迭代次数。

So far i have this: 到目前为止,我有这个:

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

Using only a tolerance criteria, displaying the result without any rounding: 仅使用公差标准,不显示任何舍入结果:

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

}

UPDATE : it'll will display: 更新 :它将显示:

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

Use this instead: 使用此代替:

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

and the result is: 结果是:
the value of pi is: 3.1425916543395442 pi的值是: 3.1425916543395442
the number of iterations for pi value is 1001 pi值的迭代次数为1001

Now, if you want to reach an exact precision, do like this: 现在,如果要达到精确的精度,请执行以下操作:
Define an acceptable error, in your case, you need need to be 3.141592. 定义一个可接受的错误,您的情况需要为3.141592。 Therefore, you need your tolerated error be less than 0.0000001. 因此,您需要允许的误差小于0.0000001。 Change the above code as below: 更改上面的代码,如下所示:

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

And the result is : 结果是:

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

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

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