新浪博客

用马青公式计算π的Java代码实际例子

2023-10-06 13:48阅读:
用马青公式计算 π 的 Java 代码实际例子
//////////////////////////////////////////////
// Bigpi.java
//////////////////////////////////////////////////
package bigpi;
import java.math.BigDecimal;
import java.io.* ;
public class Bigpi {
public static void main(String args[]) throws IOException{
String I1, I2 ,If;
BigDecimal one, sixteen, four, oneby5, oneby239, atn15, atn239, xsq, powercum, bigpi;
PrintWriter dos = new PrintWriter(System.out, true) ;
System.out.println(' ------------------------------------ ');
System.out.println(' Calculation of Pi by Machin method ');
System.out.println(' ------------------------------------ ');
System.out.println('
by M. Gallant 10/16/97 ');
System.out.println(' ');
try (BufferedReader disys = new BufferedReader(new InputStreamReader(System.in))) {
System.out.println('Enter digits of precision to calculate PI:') ;
I1=disys.readLine() ;

int tscale = Integer.parseInt(I1) +2;

one = new BigDecimal('1') ;
four = new BigDecimal('4') ;
sixteen = new BigDecimal('16') ;
oneby5 = new BigDecimal('0.2') ;
oneby239 = one.divide(new BigDecimal('239'), tscale, BigDecimal.ROUND_DOWN) ;

//calculate arctan(1/5) to tscale digits of precision:
// Based on log10(1/5) need about 1.5*tscale maximum exponent.

atn15 = oneby5 ; // initialize to first term of arctan series.
powercum = oneby5; // start with first power.
xsq = oneby5.multiply(oneby5) ;

System.out.println('Starting Calculation with '+tscale+' terms ....') ;
for (int i=3; i<=(3*tscale/2); i+=4) {
powercum = powercum.multiply(xsq) ;
atn15 = atn15.subtract(powercum.divide(new BigDecimal(String.valueOf(i)), tscale, BigDecimal.ROUND_DOWN));
powercum = powercum.multiply(xsq) ;
atn15 = atn15.add(powercum.divide(new BigDecimal(String.valueOf(i+2)), tscale, BigDecimal.ROUND_DOWN));
atn15 = atn15.setScale(tscale, BigDecimal.ROUND_DOWN) ;
}

//calculate arctan(1/239) to tscale digits of precision:
// Based on log10(1/239) need about 0.5*tscale maximum exponent.
atn239 = oneby239 ; // initialize to first term of arctan series.
powercum = oneby239; // start with first power.
xsq = oneby239.multiply(oneby239) ;

for (int i=3; i<=tscale/2; i+=4) {
powercum = (powercum.multiply(xsq)).setScale(tscale, BigDecimal.ROUND_DOWN) ;
atn239 = atn239.subtract(powercum.divide(new BigDecimal(String.valueOf(i)), BigDecimal.ROUND_DOWN));
powercum = (powercum.multiply(xsq)).setScale(tscale, BigDecimal.ROUND_DOWN) ;
atn239 = atn239.add(powercum.divide(new BigDecimal(String.valueOf(i+2)), BigDecimal.ROUND_DOWN));
atn239 = atn239.setScale(tscale, BigDecimal.ROUND_DOWN) ;
}


bigpi = sixteen.multiply(atn15) ;
bigpi = bigpi.subtract(four.multiply(atn239)) ;

System.out.println('BigPi (terms summed= '+tscale+')' + bigpi) ;
// System.out.println('Pi: ' + Math.PI) ;


}
}
}
///////////////////////////////////////////////////////
在win8.1x64 及 NeibeansIDE8.0中调试运行,通过。
注意,用了BigDecimal基础类库,即数字字符串--任意精度技术。
准备在下一篇根据个人体会,解释一下。
此程序主要是比较简单易读。业余玩一玩可以。
运算位数在1万位不到1分钟。3万位已经很吃力了,约7分钟。
比起Pifast小软件差得远了,它可以达到亿位,轻而易举。它用的是丘德诺夫斯基公式。
最近又有一种多线程的 y-cruncher 比 pifast 快。但其结果数据的记事本文件半天打不开,可用
UltraEdit轻松打开。据该网站介绍,主要供大型计算,如多少天之类的超大位数计算用的。家用
电脑计算到5万位也很轻松。和pifast相比,速度快了许多。因为用了多线程、多核处理的优点。
这是它的亮点。y-cruncher的作者是华裔美国人余智恒,即 Alexandar.J.Tee,祖籍广东台山。
////////////////////////////////////////////////////////
此程序BigPi运行结果与小软件pifast运行结果的对比,看看有无数据差别:
1000位pi值计算结果


用马青公式计算π的Java代码实际例子
用马青公式计算π的Java代码实际例子
用马青公式计算π的Java代码实际例子
用马青公式计算π的Java代码实际例子
对比两者结果,完全相同。
接下篇

我的更多文章

下载客户端阅读体验更佳

APP专享