# Calculating pi with BigDecimal - problem

• 03-13-2012, 01:35 AM
bleuprints
Calculating pi with BigDecimal - problem
Hello,

I have a problem with my program that I've written:
I try to calculate pi as exact as possible with Leibniz' algorithm:

(pi/4)=(1/1)-(1/3)+(1/5)-(1/7)+(1/9).... etc.
Quickly I discovered I would need exacter numbers so I decided to use BigDecimal.
However when calculating the next term (e.g. -1/3 or 1/5) it does not calculate it correctly.

This is my code:

import java.util.Scanner;
import java.math.*;

public class Leibniz{

public static void main(String[] args)

{
Scanner in = new Scanner(System.in);
System.out.println();
System.out.println();
System.out.print("Give the number of steps to calculate pi: ");
int approx = in.nextInt();
BigDecimal approximate = new BigDecimal(0);

for (int i = 1; i<=approx; i++)
{
if(i%2==1){
BigDecimal accuracy = new BigDecimal(1/((2*i)-1));
System.out.println(accuracy);
}
else {
BigDecimal accuracy = new BigDecimal(-1/((2*i)-1));
System.out.println(accuracy);

}

}
BigDecimal final = approximate.multiply(new BigDecimal(4));
System.out.println();
System.out.print("Your approximation for pi is: ");
System.out.print(final);

}}

I put some system.out.println's to check where it goes wrong and I think it must be in the bold part....

Could anyone help me out?

• 03-13-2012, 02:08 AM
Norm
Re: Calculating pi with BigDecimal - problem
What is the reason for using BigDecimal?
What was the problem using int or long?
• 03-13-2012, 10:01 AM
bleuprints
Re: Calculating pi with BigDecimal - problem
I dont use BigInteger but BigDecimal because calculating pi requires many ciphers after the comma.
• 03-13-2012, 10:27 AM
JosAH
Re: Calculating pi with BigDecimal - problem
Do you know what integer division does? e.g. 5/3 == 1 and 1/2 == 0

kind regards,

Jos
• 03-13-2012, 11:05 PM
bleuprints
Re: Calculating pi with BigDecimal - problem
Code:

```import java.util.Scanner;  import java.math.*; public class Leibniz{     public static void main(String[] args)     {         Scanner in = new Scanner(System.in);         System.out.println();         System.out.println();         System.out.print("geef aan hoeveel stappen je wilt zetten om pi te benaderen: ");         int benader = in.nextInt();         BigDecimal benadering = new BigDecimal(0);                 for (int i = 1; i<=benader; i++)         {             BigDecimal b = new BigDecimal(i);             BigDecimal c = b.add(b);             BigDecimal d = c.add(new BigDecimal(-1));             BigDecimal x = new BigDecimal(1);             BigDecimal y = new BigDecimal(-1);             if(i%2==1){             BigDecimal accuracy = x.divide(d);             System.out.println(accuracy);             benadering = benadering.add(accuracy);             }             else {             BigDecimal accuracy = y.divide(d);             System.out.println(accuracy);             benadering = benadering.add(accuracy);             }                     }         BigDecimal eindbenadering = benadering.multiply(new BigDecimal(4));         System.out.println();         System.out.print("Je benadering voor pi is:");         System.out.print(eindbenadering);           }}```
This is my new code but it gives the following error:
" at Leibniz.main(Leibniz.java:25)
java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result.
at java.math.BigDecimal.divide(BigDecimal.java:1603)
at Leibniz.main(Leibniz.java:30)
"

I replaced all the int's with BigDecimals so I could divide, multiply etc....
• 03-14-2012, 01:04 AM
bleuprints
Re: Calculating pi with BigDecimal - problem
Thank you all for your help!
I use doubles now, a friend pointed out that computing more than 15 decimals takes too long anyway :p