Results 1 to 4 of 4
 12182010, 05:52 AM #1Member
 Join Date
 Oct 2010
 Posts
 4
 Rep Power
 0
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; } }
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); }
 12182010, 08:10 AM #2
 Join Date
 Sep 2008
 Location
 Voorschoten, the Netherlands
 Posts
 14,271
 Blog Entries
 7
 Rep Power
 24
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,
JosThe only person who got everything done by Friday was Robinson Crusoe.
 12252010, 10:19 PM #3
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; 12262010 at 02:52 AM.

Similar Threads

reversing digits
By gandalf5166 in forum New To JavaReplies: 3Last Post: 03062010, 05:34 PM 
Digits of an integer.
By Allgorythm in forum New To JavaReplies: 8Last Post: 01012010, 03:34 AM 
split() by digits
By RobertF in forum New To JavaReplies: 2Last Post: 03122009, 03:16 AM 
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03082009, 02:51 AM 
sum of digits depreciation
By jleas in forum New To JavaReplies: 13Last Post: 11092008, 02:37 PM
Bookmarks