如何做BBP算法的基数16以获取PI [英] How to do base 16 for BBP algorithm to get PI
问题描述
我正在使用以下代码来计算Pi,但我得到了错误的答案,因为该公式某些使用基数16的方式.Wikipedia说
I am using the following code to calculate Pi but I get the wrong answer because the formula some how uses base 16. Wikipedia says here that formula has to do with hexadecimal but I don't know which part of formula process. What part of the formula do you use base 16, is it the input of the formula or just the output? For eg Do I convert k to base 16 before entering k into the formula or do I input normal base 10 number into formula and convert output of formula to base 10 from base 16. The wrong answer gives a few decimal places wrong for this code:
import java.io.Console;
public class Main {
public static void main(String[] args) {
System.out.println("Enter how many decimal places you want for Pi?");
String line = System.console().readLine();
double x = Double.parseDouble(line);
double y = 0.0;
for(double i = 0; i <= x; i++)
{
y = y + (1.0/Math.pow(16.0,i))*((4.0/(8.0*i + 1.0))-(2.0/(8.0*i + 4.0)) -
(1.0/(8.0*i + 5.0)) - (1.0/(8.0*i + 6.0)));
}
System.out.println(y);
}
}
推荐答案
您的代码正确实现了Bailey–Borwein–Plouffe公式.它计算的π估计随着您使用的迭代次数的增加而逐渐增加:
Your code implements the Bailey–Borwein–Plouffe formula correctly. It calculates an estimation of π that increases in accurary the more iterations you use:
- 输入
1
,您将得到:3.1414224664224664
- 输入
2
,您将得到:3.1415873903465816
- 输入
3
,您将得到:3.1415924575674357
- 输入
4
,您将得到:3.1415926454603365
- 输入
5
,您将得到:3.141592653228088
- 输入
6
,您将得到:3.141592653572881
- 输入
7
,您将得到:3.141592653588973
- 输入
8
,您将得到:3.1415926535897523
- 输入
9
,您将得到:3.1415926535897913
- 输入
10
,您将得到:3.141592653589793
- 输入
11
,您将得到:3.141592653589793
- Enter
1
, you get:3.1414224664224664
- Enter
2
, you get:3.1415873903465816
- Enter
3
, you get:3.1415924575674357
- Enter
4
, you get:3.1415926454603365
- Enter
5
, you get:3.141592653228088
- Enter
6
, you get:3.141592653572881
- Enter
7
, you get:3.141592653588973
- Enter
8
, you get:3.1415926535897523
- Enter
9
, you get:3.1415926535897913
- Enter
10
, you get:3.141592653589793
- Enter
11
, you get:3.141592653589793
就是这样.任何更高的输入都是无用的,因为使用double
时无法获得更精确的输入.
And that's it. Any higher input is useless, because you can't get more precise when you use double
.
如果要获得更高的精度,则应使用比double
更高精度的类型,例如BigDecimal
:
If you want to get more precision, you should use a type with more precision than double
, e.g. BigDecimal
:
BigDecimal pi = BigDecimal.ZERO;
for (int i = 0; i <= x; i++) {
BigDecimal a = BigDecimal.valueOf(1).divide(BigDecimal.valueOf(16).pow(i), 30, RoundingMode.HALF_UP);
BigDecimal b1 = BigDecimal.valueOf(4).divide(BigDecimal.valueOf(8).multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(1)), 30, RoundingMode.HALF_UP);
BigDecimal b2 = BigDecimal.valueOf(2).divide(BigDecimal.valueOf(8).multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(4)), 30, RoundingMode.HALF_UP);
BigDecimal b3 = BigDecimal.valueOf(1).divide(BigDecimal.valueOf(8).multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(5)), 30, RoundingMode.HALF_UP);
BigDecimal b4 = BigDecimal.valueOf(1).divide(BigDecimal.valueOf(8).multiply(BigDecimal.valueOf(i)).add(BigDecimal.valueOf(6)), 30, RoundingMode.HALF_UP);
BigDecimal b = b1.subtract(b2).subtract(b3).subtract(b4);
pi = pi.add(a.multiply(b));
}
- 输入
10
,您将得到:3.141592653589793129614170564040940187329961242598487417146011
- 输入
100
,您将得到:3.141592653589793238462643383279097710627677399356070777774427
- Enter
10
, you get:3.141592653589793129614170564040940187329961242598487417146011
- Enter
100
, you get:3.141592653589793238462643383279097710627677399356070777774427
您使用的精度越高(在此由BigDecimal
的divide
方法中的参数30
设置),并且运行的迭代次数越多,π的近似值就越好.
The more precision you use (here set by the parameter 30
in the divide
method of BigDecimal
) and the more iterations you run, the better the approximation of π will be.
这篇关于如何做BBP算法的基数16以获取PI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!