簡體   English   中英

使用Leibniz系列求pi值

[英]Finding pi value using Leibniz series

我開始學習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);
            }
        }
    }
}

僅使用公差標准,不顯示任何舍入結果:

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

使用此代替:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM