Java 用莱布尼兹级数求pi值
我开始学习Java,我的代码有问题 当然,它有明显的错误:它没有运行。我被要求用莱布尼兹级数求pi值,以及达到六位有效数字(3.141592)的迭代次数 到目前为止,我有:Java 用莱布尼兹级数求pi值,java,Java,我开始学习Java,我的代码有问题 当然,它有明显的错误:它没有运行。我被要求用莱布尼兹级数求pi值,以及达到六位有效数字(3.141592)的迭代次数 到目前为止,我有: public class Findingpie2 { public static void main(String[] args) { double pi = 0.0; int counter = 1; for (int n = 0; n < counter; n
public class Findingpie2 {
public static void main(String[] args) {
double pi = 0.0;
int counter = 1;
for (int n = 0; n < counter; n++) {
pi += Math.pow(-1, n) / (2*n + 1);
counter++;
if (pi==3.141592) {
System.out.println("the value of pi is: "+String.format("%6f",4*pi));
System.out.println("the number of iterations for pi value is "+n);
}
}
}
}
公共类查找PIE2{
公共静态void main(字符串[]args){
双pi=0.0;
int计数器=1;
用于(int n=0;n
仅使用公差标准,显示结果而不进行任何舍入:
package dummy;
import static java.lang.String.format;
import static java.lang.System.out;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.Map.Entry;
/*
* Finding pi value using Leibniz series
*
* The Leibniz series is converging. To compare two successive values
* is enough to get the required precision.
*
* http://stackoverflow.com/questions/34834854/finding-pi-value-using-leibniz-serie
*
*/
public class Findingpie2 {
public static void main(String[] args) {
out.println("Pi, no rounding:");
for (int i = 2; i < 10; i++) {
double tolerance = Math.pow(0.1, i);
Entry<Integer, Double> result = calcpi(tolerance);
String pi = result.getValue().toString().substring(0, i+1);
out.println(format("The value of pi is: %s with %." + i + "f tolerance (%d iterations)." , pi, tolerance, result.getKey()));
}
}
private static Entry<Integer, Double> calcpi(double tolerance) {
int n = 0;
double pi = 0;
double bpi = 10 * tolerance;
double inc = 1;
while (Math.abs(bpi - pi) > tolerance) {
bpi = pi;
pi += inc / (2*n + 1);
inc = -inc;
n++;
}
return new SimpleImmutableEntry<Integer, Double>(n, 4 * pi);
}
}
改用这个:
public static void main(String[] args) {
double pi = 0.0;
int MAX_ITERATIONS=1000;
int n=0;
while(true) {
pi += Math.pow(-1, n) / (2*n + 1);
n++;
if (n>MAX_ITERATIONS) {
System.out.println("the value of pi is: "+String.valueOf(4*pi));
System.out.println("the number of iterations for pi value is "+n);
break;
}
}
}
结果是:pi的值为:3.1425916543395442
pi值的迭代次数为1001 现在,如果要达到精确的精度,请执行以下操作:
定义一个可接受的错误,在您的情况下,您需要是3.141592。因此,您需要允许的错误小于0.0000001。将上述代码更改如下:
public static void main(String[] args) {
double pi = 0.0;
double ERROR = 0.0000001;
int n=0;
while(true) {
pi += Math.pow(-1, n) / (2*n + 1);
n++;
if (Math.abs(4*pi-Math.PI)<ERROR) {
System.out.println("the value of pi is: "+String.valueOf(4*pi));
System.out.println("the number of iterations for pi value is "+n);
break;
}
}
}
请详细说明“它不运行”——它实际上是做什么的?请注意,您遇到的一个大问题是,您的if条件几乎永远不会为真,因为double不能像那样工作。由于计算机是由数字逻辑部件构成的,这些部件将数据表示为开或关位,因此这些类型的计算机无法以全精度表示浮点数,因此您的等式几乎肯定会失败。提供一个范围要好得多。什么都不做。那么我应该为π指定一个介于3和4之间的范围吗?介于3和4之间有意义吗?不太准确,因为这不太准确,是吗?尝试一些更接近3.141592的东西。如果我没有弄错的话,你的循环实际上是无限运行的。每次迭代都会增加
计数器
和n
,因此即使打印出来,它也会继续运行。您可能需要添加一个中断
在if
语句结尾处终止循环是的,您是对的,但我不允许使用break。错误界限0.0000001
小于pi近似值的精度3.141592
。我建议您使用Math.PI
而不是3.141592
。使用搜索值知道何时停止搜索的算法是什么;-)。对于任何实际算法,结束标准都不能使用搜索的目标。你是对的,正如算法所建议的,n应该尽可能大,理论上,趋于无穷大。然而,这个问题需要检查它何时达到合理的精度,比如pi。请注意,我们正在估算一个带和的积分,我们知道这个特定情况下的确切答案,pi。我对Math.pi不太熟悉。你的算法会让你更聪明sense@Falahati如果您想检查Leibniz算法的行为,即根据所需精度确定的迭代次数,请参阅我的回答。
public static void main(String[] args) {
double pi = 0.0;
double ERROR = 0.0000001;
int n=0;
while(true) {
pi += Math.pow(-1, n) / (2*n + 1);
n++;
if (Math.abs(4*pi-Math.PI)<ERROR) {
System.out.println("the value of pi is: "+String.valueOf(4*pi));
System.out.println("the number of iterations for pi value is "+n);
break;
}
}
}
the value of pi is: 3.1415919000000936
the number of iterations for pi value is 1326982