Results 1 to 4 of 4
  1. #1
    doymand is offline Member
    Join Date
    Oct 2010
    Posts
    4
    Rep Power
    0

    Default Errors in digits of calculating pi

    This class runs, compiles and such, but its output is a 'bit' off. It gives the value of 3.1583289............

    My theory is that its because transfer of doubles to BigDecimal. I did this because it makes it easier because you can just use operators simplifying the code a bit. I used the method valueOf to possibly fix it, but there was no change in the output.

    If that's the problem is there any way around it with minimal changes or do I have to convert everything to BigDecimal in order to get accurate output?Looking for some advice as to if the problem is what I think it is or something else and help on fixing it.


    Java Code:
    import java.math.BigDecimal;
    
    public class pi
    {
      private long nums = 0;
      private String out;
      private String test1; //*******************************
      private String test2; // Temporary strings for testing 
      private String test3; //******************************** 
      
      
      BigDecimal pi = new BigDecimal(0); //Final Answer
      BigDecimal arc1 = new BigDecimal(0); //Arctan of 1/5
      BigDecimal arc2 = new BigDecimal(0);//Arctan of 1/239
      BigDecimal FOUR = new BigDecimal("4"); //Constant used to multiply
      
      
    
      public pi(long run) //Gets input for user for number of iterations of program
      {
        nums = run;
      }
      
      private void calcArc1 () //1/5
      {
        BigDecimal temp = new BigDecimal("0");
        double arctan = 0;
        
        for (long count = 0; count <= nums; count++)
        {
          arctan = (((Math.pow(-1,count))*(Math.pow(.2,2*count+1)))/(2*count+1));
        
      
      
    
        temp = temp.valueOf(arctan);
          arc1 = arc1.add(temp);
        }
        
        arc1 = arc1.multiply(FOUR);
        
        
      }
        
        private void calcArc2 () //1/239
        {
             BigDecimal temp = new BigDecimal("0");
          double arctan = 0;
      
          for (long count = 0; count <= nums; count++)
          {
            arctan = (((Math.pow(-1,count))*(Math.pow(1/239,2*count+1)))/(2*count+1));
          
       
          temp = temp.valueOf(arctan);
         
            arc2 = arc2.add(temp);
          }
     
        }
        
        public void calcPi ()
        {
    
          calcArc2();
          calcArc1(); 
          pi = arc1.subtract(arc2);
          pi = pi.multiply(FOUR);
      
          
        }
         public String toString()
         {
           
           out = "Pi is " + pi;
        
           
           return out;
         }
    }
    And the code that runs it



    Java Code:
    import java.util.Scanner;
    
    public class picalc
    {
      public static void main (String [] args)
      {
        long iterations = 0;
        Scanner scan = new Scanner(System.in);
        iterations = scan.nextLong();
        pi answer = new pi(iterations);
        
        answer.calcPi();
    
        
        System.out.println(answer);
      }

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

    Default

    You are doing all your calculations in type double; only the intermediate results are cast to a BigDecimal type. See this page how to do all calculations in type BigDecimal.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    That sounds an awful lot like a lie and an advertisement in an inactive thread.

    Hmm.......


    Note: Please ignore this post as it has been remedied by a moderator.
    Last edited by Zack; 12-26-2010 at 02:52 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. reversing digits
    By gandalf5166 in forum New To Java
    Replies: 3
    Last Post: 03-06-2010, 05:34 PM
  2. Digits of an integer.
    By Allgorythm in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 03:34 AM
  3. split() by digits
    By RobertF in forum New To Java
    Replies: 2
    Last Post: 03-12-2009, 03:16 AM
  4. Replies: 3
    Last Post: 03-08-2009, 02:51 AM
  5. sum of digits depreciation
    By jleas in forum New To Java
    Replies: 13
    Last Post: 11-09-2008, 02:37 PM

Posting Permissions

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