Results 1 to 6 of 6
 03132012, 01:35 AM #1Member
 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);
approximate = approximate.add(accuracy);
}
else {
BigDecimal accuracy = new BigDecimal(1/((2*i)1));
System.out.println(accuracy);
approximate = approximate.add(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?
Thnx in advance!
 03132012, 02:08 AM #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; 03132012 at 01:27 PM. Reason: changed Integer to Decimal
 03132012, 10:01 AM #3Member
 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.
 03132012, 10:27 AM #4
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 13,881
 Blog Entries
 7
 Rep Power
 22
Re: Calculating pi with BigDecimal  problem
Do you know what integer division does? e.g. 5/3 == 1 and 1/2 == 0
kind regards,
Joscenosillicaphobia: the fear for an empty beer glass
 03132012, 11:05 PM #5Member
 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: "); 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); }}
" at Leibniz.main(Leibniz.java:25)
java.lang.ArithmeticException: Nonterminating 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; 03132012 at 11:08 PM. Reason: added code tags
 03142012, 01:04 AM #6Member
 Join Date
 Mar 2012
 Posts
 4
 Rep Power
 0
Similar Threads

BigDecimal
By tarka in forum New To JavaReplies: 4Last Post: 11122011, 09:22 PM 
Calculating the distance between two points problem
By silverglade in forum New To JavaReplies: 16Last Post: 05142011, 08:23 PM 
Using BigDecimal
By doymand in forum New To JavaReplies: 2Last Post: 10302010, 09:25 AM 
Having problem in calculating leap year
By lclclc in forum New To JavaReplies: 3Last Post: 09252009, 08:50 PM 
PROBLEM  calculating with array elements
By ella in forum New To JavaReplies: 13Last Post: 12042008, 01:36 AM
Bookmarks