# Thread: Calculating pi with BigDecimal - problem

1. Member
Join Date
Mar 2012
Posts
4
Rep Power
0

## 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?

2. ## Re: Calculating pi with BigDecimal - problem

What is the reason for using BigDecimal?
What was the problem using int or long?
Last edited by Norm; 03-13-2012 at 01:27 PM. Reason: changed Integer to Decimal

3. Member
Join Date
Mar 2012
Posts
4
Rep Power
0

## Re: Calculating pi with BigDecimal - problem

I dont use BigInteger but BigDecimal because calculating pi requires many ciphers after the comma.

4. ## 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

5. Member
Join Date
Mar 2012
Posts
4
Rep Power
0

## Re: Calculating pi with BigDecimal - problem

Java 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: ");

for (int i = 1; i<=benader; i++)
{
BigDecimal b = new BigDecimal(i);
BigDecimal x = new BigDecimal(1);
BigDecimal y = new BigDecimal(-1);
if(i%2==1){
BigDecimal accuracy = x.divide(d);
System.out.println(accuracy);
}
else {
BigDecimal accuracy = y.divide(d);
System.out.println(accuracy);
}

}
System.out.println();

}}```
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....
Last edited by Norm; 03-13-2012 at 11:08 PM. Reason: added code tags

6. Member
Join Date
Mar 2012
Posts
4
Rep Power
0

## 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

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•