使用java BigDecimal完成圆周率π的计算

类别:Java 点击:0 评论:0 推荐:

package chu_nz_baby;

import java.util.*;
import java.math.*;

/**
 * <p>Title:π 圆周率的计算 </p>
 * <p>Description:使用java BigDecimal完成圆周率(Chudnovsky公式)的计算。可以指定计算精度和计算参数。 </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <a href="http://www.jason314.com/palgorithm.htm">圆周率的计算方法#Chudnovsky公式</a>
 * <a href="http://www.oursci.org/magazine/200301/030126.htm">圆周率π的计算历程</a>
 * @author [email protected]
 * @version 1.0
 */

public class Pi {

  private BigDecimal result;

  public Pi() {

  }

  public void run(int n, int scale) {
    if(System.getProperty("debug") != null){
        System.out.println("n=" + n);
    }

    BigDecimal upper = new BigDecimal(426880.0000 * Math.sqrt(10005.0000));
    upper = upper.setScale(scale, BigDecimal.ROUND_HALF_EVEN);

    if(System.getProperty("debug") != null){
        System.out.println("upper=" + upper);
    }

    BigDecimal downer = BigDecimal.valueOf(0);

    for (int i = 0; i <= n; i++) {
      BigDecimal d0 = factorial(6 * i).multiply ( new BigDecimal( 545140134 * i + 13591409));
      BigDecimal d1 = factorial(i);
      BigDecimal d2 = pow(d1, 3);
      BigDecimal d3 = factorial(3 * i);
      BigDecimal d4 = pow(-640320, 3 * i);
      BigDecimal d5 = d2.multiply(d3);
      BigDecimal d6 = d4.multiply(d5);
      BigDecimal d = d0.divide( d6, BigDecimal.ROUND_HALF_EVEN );

      downer = downer.add(d);

    }

    if(System.getProperty("debug") != null){
             System.out.println("downer=" + downer);
    }

   if(System.getProperty("debug") != null){
     System.out.println("upper.scale = " + upper.scale());
     System.out.println("downer.scale = " + downer.scale());
   }

    result = upper.divide(downer, BigDecimal.ROUND_HALF_EVEN);

    if(System.getProperty("debug") != null){
      System.out.println("result.scale = " + result.scale());
    }

  }

  public BigDecimal getResult() {

    return result;
  }

  public static BigDecimal pow(double val, int power){
    return pow(new BigDecimal(val), power);
  }

  public static BigDecimal pow(BigDecimal val, int power){
    BigDecimal value = BigDecimal.valueOf(1);
    for(int i = 1; i <= power; i ++){
      value = value.multiply(val);
    }
    return value;
  }

  public static BigDecimal factorial(int n){
    BigDecimal value = BigDecimal.valueOf(1);
    for(int i = 1; i <=n ; i++){
      value = value.multiply(BigDecimal.valueOf(i));
    }
    return value;
  }

  public static void main(String[] args) {
    if(args.length < 2){
       System.out.println("usage: " + "java nz-baby.Pi " + " 参数(0-无穷大) 精度(1-4294967296)");
       System.out.println("usage: " + "java nz-baby.Pi " + " n(0- ) scale(1-4294967296)");
       System.exit(-1);
    }

    int n = Integer.parseInt(args[0]);
    int scale = Integer.parseInt(args[1]);

    System.out.println("palgorithm n=" + n + " scale=" + scale);
    System.out.println("start:" + new Date(System.currentTimeMillis()));
    Pi p = new Pi();
    p.run(n, scale);
    System.out.println("end:" + new Date(System.currentTimeMillis()));
    System.out.println("result=" + p.getResult());
  }

}

本文地址:http://com.8s8s.com/it/it16579.htm