Results 1 to 6 of 6
Thread: pi and the wallis' product
 03092009, 01:15 AM #1Member
 Join Date
 Mar 2009
 Posts
 4
 Rep Power
 0
pi and the wallis' product
I'm having trouble getting to pi (3.14159)using the wallis' product (2*2/3*4/3 etc.) I think there is something wrong with the division but I'm not sure how to fix it. Anyway here is what I have. Any help would be appreciated. Thanks!
Java Code:public static void main(String [] args) { int nextDenominator = 1; int nextNumerator = 2; int termCount = 1; float half_pi = 1.0f; while(termCount < 1000000) { half_pi *= nextNumerator / nextDenominator; nextDenominator += 2; half_pi *= nextNumerator / nextDenominator; nextNumerator += 2; if (half_pi * 2 == 3.14159) break; else termCount++; } System.out.println("half_pi = " + (half_pi * 2)); System.out.println("termCount = " + termCount); }
 03092009, 01:35 AM #2Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,704
 Rep Power
 13
I think there is something wrong with the division
/ performs integer division when it has two integer arguments  and I don't think this is what you want here.
It usual to use double as your floating point type, rather than float.
 03092009, 04:23 AM #3Member
 Join Date
 Mar 2009
 Posts
 4
 Rep Power
 0
I removed the integers and changed termCount and half_pi to doubles. However I still get half_pi = 2.0. This tells me that half_pi is equal to 1.0 until termCount reaches 1000000 then it multiplies half_pi by 2.
Java Code:public static void main(String[] args) { double termCount = 2.0; double half_pi = 1.0; while(termCount < 1000000) { half_pi *= termCount / termCount; half_pi *= termCount / termCount++; if (half_pi * 2.0 == 3.14159) break; else termCount += 2; } System.out.println("termCount = " + termCount); half_pi *= 2.0; System.out.println("half_pi = " + half_pi); }
 03092009, 07:22 AM #4Senior Member
 Join Date
 Dec 2008
 Posts
 526
 Rep Power
 0
whay don't just use Math lib here?
 03092009, 09:11 AM #5Senior Member
 Join Date
 Dec 2008
 Location
 Hong Kong
 Posts
 473
 Rep Power
 7
do you know 5 / 7 is 0
5 / 7.0 is 0.714....
for nextDenominator and nextNumerator, please declare them as double or float
 03092009, 11:57 PM #6Moderator
 Join Date
 Feb 2009
 Location
 New Zealand
 Posts
 4,704
 Rep Power
 13
Java Code:half_pi *= termCount / termCount; half_pi *= termCount / termCount++;
Try this:
Java Code:public static void main(String args[]) { double foo = 10; System.out.print(foo / foo); }
One way to fix this would be you use more variables, eg explicit numerator and denominator variables. This would enable you to print out and see exactly what is being divided by what.
Similar Threads

I need some basic Java Script help I am using a product vbulletin and run in to a jam
By Frank H. Shaw in forum New To JavaReplies: 1Last Post: 03012009, 06:26 PM 
Finished Product: What now?
By Unome in forum Java AppletsReplies: 5Last Post: 02112009, 11:41 AM 
Product update service for Java application.
By Eranga in forum Advanced JavaReplies: 4Last Post: 04222008, 09:24 AM 
sum,product,average program
By chitwood in forum New To JavaReplies: 7Last Post: 01242008, 06:18 AM 
can anyone help me to integrate license manager to a java product
By shwetatalikoti in forum NetBeansReplies: 0Last Post: 10292007, 06:34 AM
Bookmarks