题目要求 利用给定公式求PI的值
package 级数求和;
public class progression {
public static void main(String[] args) {
double pi;
pi=16*jishu(1.0/5)-4*jishu(1.0/239);
System.out.printf("%.15f",pi);
}
public static void main(String[] args) {
double pi;
pi=16*jishu(1.0/5)-4*jishu(1.0/239);
System.out.printf("%.15f",pi);
}
//定义求级数函数
public static double jishu(double x) {
// TODO Auto-generated method stub
double n,m;
int i=0;
n=0;
m=1;
for(i=0;Math.abs(m)>=Math.pow(10,-15);i++){
m=Math.pow(-1,i)*Math.pow(x, 2*i+1)/(double)(2*i+1);
n+=m;
}
return n;
}
}
public static double jishu(double x) {
// TODO Auto-generated method stub
double n,m;
int i=0;
n=0;
m=1;
for(i=0;Math.abs(m)>=Math.pow(10,-15);i++){
m=Math.pow(-1,i)*Math.pow(x, 2*i+1)/(double)(2*i+1);
n+=m;
}
return n;
}
}
本文介绍了一个使用Java编程语言实现的π值计算程序。通过定义一个级数求和的方法并运用特定公式来逼近π的值,该程序展示了如何利用数学级数进行数值计算。最终输出的π值精确到小数点后15位。
1293

被折叠的 条评论
为什么被折叠?



