在 Android 手机上计算 Pi
我正在尝试让 Android 手机计算 Pi 的 n 位小数。为此,我使用这段代码:
public class Pi {
private static final BigDecimal TWO = new BigDecimal(2);
private static final BigDecimal FOUR = new BigDecimal(4);
private String nummer;
public void pi(final int SCALE) {
BigDecimal a = ONE;
BigDecimal b = ONE.divide(sqrt(TWO, SCALE), SCALE, ROUND_HALF_UP);
BigDecimal t = new BigDecimal(0.25);
BigDecimal x = ONE;
BigDecimal y;
while (!a.equals(b)) {
y = a;
a = a.add(b).divide(TWO, SCALE, ROUND_HALF_UP);
b = sqrt(b.multiply(y), SCALE);
t = t.subtract(x.multiply(y.subtract(a).multiply(y.subtract(a))));
x = x.multiply(TWO);
}
a.add(b).multiply(a.add(b)).divide(t.multiply(FOUR), SCALE, ROUND_HALF_UP);
number(a);
}
public void number(BigDecimal a) {
//nummer = a.toString().length();
nummer = a.toString();
}
public String returnNum() {
return nummer;
}
public static BigDecimal sqrt(BigDecimal A, final int SCALE) {
BigDecimal x0 = new BigDecimal("0");
BigDecimal x1 = new BigDecimal(Math.sqrt(A.doubleValue()));
while (!x0.equals(x1)) {
x0 = x1;
x1 = A.divide(x0, SCALE, ROUND_HALF_UP);
x1 = x1.add(x0);
x1 = x1.divide(TWO, SCALE, ROUND_HALF_UP);
}
return x1;
}
这在任何计算机上都像一个魅力,但当我尝试在我的 Android 平板电脑上运行它时,它决定 Pi 是 0.8 左右。为什么会发生这种计算失误以及如何才能完成这项工作?
I'm trying to make an Android phone calculate n decimals of Pi. For this I'm using this code:
public class Pi {
private static final BigDecimal TWO = new BigDecimal(2);
private static final BigDecimal FOUR = new BigDecimal(4);
private String nummer;
public void pi(final int SCALE) {
BigDecimal a = ONE;
BigDecimal b = ONE.divide(sqrt(TWO, SCALE), SCALE, ROUND_HALF_UP);
BigDecimal t = new BigDecimal(0.25);
BigDecimal x = ONE;
BigDecimal y;
while (!a.equals(b)) {
y = a;
a = a.add(b).divide(TWO, SCALE, ROUND_HALF_UP);
b = sqrt(b.multiply(y), SCALE);
t = t.subtract(x.multiply(y.subtract(a).multiply(y.subtract(a))));
x = x.multiply(TWO);
}
a.add(b).multiply(a.add(b)).divide(t.multiply(FOUR), SCALE, ROUND_HALF_UP);
number(a);
}
public void number(BigDecimal a) {
//nummer = a.toString().length();
nummer = a.toString();
}
public String returnNum() {
return nummer;
}
public static BigDecimal sqrt(BigDecimal A, final int SCALE) {
BigDecimal x0 = new BigDecimal("0");
BigDecimal x1 = new BigDecimal(Math.sqrt(A.doubleValue()));
while (!x0.equals(x1)) {
x0 = x1;
x1 = A.divide(x0, SCALE, ROUND_HALF_UP);
x1 = x1.add(x0);
x1 = x1.divide(TWO, SCALE, ROUND_HALF_UP);
}
return x1;
}
This works like a charm on any computer but when I try to run this on my Android tablet it decides that Pi is 0.8 something. Why does this miss calculation occur and how can I make this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的计算遗漏了一点。该行 :
没有被分配给任何东西。将其分配给某物并打印该值,您会发现您的计算有效。
这给了我正确的答案。
对于那些感兴趣的人,可以在 wikipedia
You are missing a bit from your calculation. The line :
is not being assigned to anything. Assign this to something and print that value out and you'll find your calculation works.
This gives me the correct answer.
For those that are interested, the algorithm can be found on wikipedia