Results 1 to 6 of 6
  1. #1
    bleuprints is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Question 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!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default 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 12:27 PM. Reason: changed Integer to Decimal

  3. #3
    bleuprints is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default Re: Calculating pi with BigDecimal - problem

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

  4. #4
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default 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
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    bleuprints is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default 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);
           
        }}
    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 10:08 PM. Reason: added code tags

  6. #6
    bleuprints is offline Member
    Join Date
    Mar 2012
    Posts
    4
    Rep Power
    0

    Default 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

Similar Threads

  1. BigDecimal
    By tarka in forum New To Java
    Replies: 4
    Last Post: 11-12-2011, 08:22 PM
  2. Calculating the distance between two points problem
    By silverglade in forum New To Java
    Replies: 16
    Last Post: 05-14-2011, 08:23 PM
  3. Using BigDecimal
    By doymand in forum New To Java
    Replies: 2
    Last Post: 10-30-2010, 09:25 AM
  4. Having problem in calculating leap year
    By lclclc in forum New To Java
    Replies: 3
    Last Post: 09-25-2009, 08:50 PM
  5. PROBLEM - calculating with array elements
    By ella in forum New To Java
    Replies: 13
    Last Post: 12-04-2008, 12:36 AM

Posting Permissions

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