I want to calculate the value of PI till 50 digits.
How to do this in java for 50 decimal places?
I want to calculate the value of PI till 50 digits.
How to do this in java for 50 decimal places?
You cant do that with default data types, as you need for 50 digits: 50 / log(2) * log(10) = 166 bits. Here BigDecimal is one type you could use instead. But you should have in mind, that 22/7 is just an approximation of pi, and to get it right for 50 digits you need much better formula (e.g. Monte-Carlo method, taylor series, ...).
You are using a double variable and instead should use something that has a greater precision. Look into the BigDecimal
class.
public class PiReCalc {
public static final int N = 1000; // # of terms
public static void main(String[] args) {
BigDecimal sum = new BigDecimal(0); // final sum
BigDecimal term = new BigDecimal(0); // term without sign
BigDecimal sign = new BigDecimal(1.0); // sign on each term
BigDecimal one = new BigDecimal(1.0);
BigDecimal two = new BigDecimal(2.0);
for (int k = 0; k < N; k++) {
BigDecimal count = new BigDecimal(k);
//term = 1.0/(2.0*k + 1.0);
BigDecimal temp1 = two.multiply(count);
BigDecimal temp2 = temp1.add(one);
term = one.divide(temp2,50,BigDecimal.ROUND_FLOOR);
//sum = sum + sign*term;
BigDecimal temp3 = sign.multiply(term);
sum = sum.add(temp3);
sign = sign.negate();
}
BigDecimal pi = new BigDecimal(0);
BigDecimal four = new BigDecimal(4);
pi = sum.multiply(four);
System.out.println("Calculated pi (approx., " + N + " terms and 50 Decimal Places): " + pi);
System.out.println("Actual pi: " + Math.PI);
}
}
The output is
Calculated pi (approx., 1000 terms and 50 Decimal Places): 3.14059265383979292596359650286939597045138933077984
Actual pi: 3.141592653589793
Here is the break through paper of Bailey, Borwein and Plouffe: http://oldweb.cecm.sfu.ca/projects/pihex/p123.pdf
In the meantime, even faster formulas (following the same principles) were found: http://en.wikipedia.org/wiki/Bellard%27s_formula
This is a quick and dirty implementation of Bellard's formula bigPi(200,2000) is good for over 500 decimal places in 75ms.
public static BigDecimal bigPi(int max,int digits) {
BigDecimal num2power6 = new BigDecimal(64);
BigDecimal sum = new BigDecimal(0);
for(int i = 0; i < max; i++ ) {
BigDecimal tmp;
BigDecimal term ;
BigDecimal divisor;
term = new BigDecimal(-32);
divisor = new BigDecimal(4*i+1);
tmp = term.divide(divisor, digits, BigDecimal.ROUND_FLOOR);
term = new BigDecimal(-1);
divisor = new BigDecimal(4*i+3);
tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
term = new BigDecimal(256);
divisor = new BigDecimal(10*i+1);
tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
term = new BigDecimal(-64);
divisor = new BigDecimal(10*i+3);
tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
term = new BigDecimal(-4);
divisor = new BigDecimal(10*i+5);
tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
term = new BigDecimal(-4);
divisor = new BigDecimal(10*i+7);
tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
term = new BigDecimal(1);
divisor = new BigDecimal(10*i+9);
tmp = tmp.add(term.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
int s = ((1-((i&1)<<1)));
divisor = new BigDecimal(2);
divisor = divisor.pow(10*i).multiply(new BigDecimal(s));
sum = sum.add(tmp.divide(divisor, digits, BigDecimal.ROUND_FLOOR));
}
sum = sum.divide(num2power6,digits, BigDecimal.ROUND_FLOOR);
return sum;
}