Results 1 to 4 of 4
- 12-18-2010, 04:52 AM #1
Member
- 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.
And the code that runs itJava 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); }
- 12-18-2010, 07:10 AM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,424
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-25-2010, 09: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; 12-26-2010 at 01:52 AM.
-
Similar Threads
-
reversing digits
By gandalf5166 in forum New To JavaReplies: 3Last Post: 03-06-2010, 04:34 PM -
Digits of an integer.
By Allgorythm in forum New To JavaReplies: 8Last Post: 01-01-2010, 02:34 AM -
split() by digits
By RobertF in forum New To JavaReplies: 2Last Post: 03-12-2009, 02:16 AM -
What is the difference between Semantic Errors and Logical Errors?
By tlau3128 in forum New To JavaReplies: 3Last Post: 03-08-2009, 01:51 AM -
sum of digits depreciation
By jleas in forum New To JavaReplies: 13Last Post: 11-09-2008, 01:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks