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

0 篇文章 0 订阅

package chu_nz_baby;

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

/**
* <p>Title:π 圆周率的计算 </p>
* <p>Description:使用java BigDecimal完成圆周率(Chudnovsky公式)的计算。可以指定计算精度和计算参数。 </p>
* <a href="http://www.jason314.com/palgorithm.htm">圆周率的计算方法#Chudnovsky公式</a>
* <a href="http://www.oursci.org/magazine/200301/030126.htm">圆周率π的计算历程</a>
* @author chu_nz_baby@hotmail.com
* @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 );

}

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

}

02-15
05-16 1万+
07-20 1400
09-23 1905
01-18 1861
12-12 5万+
10-15 260
02-24 188
07-30 1万+
10-09 7649
11-27 4481
01-23 1万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

chunzbaby

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。