找到pi的值直到50位

时间:2011-04-06 12:17:30

标签: java math pi

我想计算PI的值直到50位。

如何在java中为50个小数位执行此操作?

5 个答案:

答案 0 :(得分:3)

您无法使用默认数据类型,因为您需要50位数:50 / log(2)* log(10)= 166位。这里BigDecimal是您可以使用的一种类型。但是你应该记住,22/7只是pi的近似值,为了得到50个数字,你需要更好的公式(例如蒙特卡罗方法,泰勒系列,......)。

答案 1 :(得分:2)

您正在使用双变量,而应使用具有更高精度的东西。查看BigDecimal类。

答案 2 :(得分:2)

public class PiReCalc {
  public static final int N = 1000; // # of terms
   public static void main(String[] args) {
  BigDecimal sum = new BigDecimal(0);      // final sum
  BigDecimal term = new BigDecimal(0);           // term without sign
  BigDecimal sign = new BigDecimal(1.0);     // sign on each term

  BigDecimal one = new BigDecimal(1.0);
  BigDecimal two = new BigDecimal(2.0);

  for (int k = 0; k < N; k++) {
     BigDecimal count = new BigDecimal(k); 
     //term = 1.0/(2.0*k + 1.0);
     BigDecimal temp1 = two.multiply(count);
     BigDecimal temp2 = temp1.add(one);
     term = one.divide(temp2,50,BigDecimal.ROUND_FLOOR);

     //sum = sum + sign*term;
     BigDecimal temp3 = sign.multiply(term);
     sum = sum.add(temp3);

     sign = sign.negate();
  }
  BigDecimal pi = new BigDecimal(0);
  BigDecimal four = new BigDecimal(4);
  pi = sum.multiply(four);

  System.out.println("Calculated pi (approx., " + N + " terms and 50 Decimal Places): " + pi);
  System.out.println("Actual pi: " + Math.PI);
   }
}

输出

计算pi(约1000个术语和50个小数位):3.14059265383979292596359650286939597045138933077984
实际pi:3.141592653589793

答案 3 :(得分:0)

以下是Bailey,Borwein和Plouffe的突破性文章:http://oldweb.cecm.sfu.ca/projects/pihex/p123.pdf

与此同时,发现了更快的公式(遵循相同的原则):http://en.wikipedia.org/wiki/Bellard%27s_formula

答案 4 :(得分:0)

这是一个快速而肮脏的实施Bellard的公式bigPi(200,2000)适用于75ms内超过500个小数位。

public static BigDecimal bigPi(int max,int digits) {
    BigDecimal num2power6 = new BigDecimal(64);
    BigDecimal sum = new BigDecimal(0);
    for(int i = 0; i < max; i++ ) {
        BigDecimal tmp;
        BigDecimal term ; 
        BigDecimal divisor;
        term = new BigDecimal(-32); 
        divisor = new BigDecimal(4*i+1); 
        tmp =  term.divide(divisor, digits, BigDecimal.ROUND_FLOOR);
        term = new BigDecimal(-1); 
        divisor = new BigDecimal(4*i+3); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(256); 
        divisor = new BigDecimal(10*i+1); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-64); 
        divisor = new BigDecimal(10*i+3); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-4); 
        divisor = new BigDecimal(10*i+5); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(-4); 
        divisor = new BigDecimal(10*i+7); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        term = new BigDecimal(1); 
        divisor = new BigDecimal(10*i+9); 
        tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
        int s = ((1-((i&1)<<1)));
        divisor = new BigDecimal(2); 
        divisor = divisor.pow(10*i).multiply(new BigDecimal(s));
        sum = sum.add(tmp.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
    }
    sum = sum.divide(num2power6,digits, BigDecimal.ROUND_FLOOR);
    return sum;

}