# Thread: pi and the wallis' product

1. Member 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);
}```  Reply With Quote

2. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## I think there is something wrong with the division
The best way to check is to put in debug statements and verify whether the code is doing what you want.

/ 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.  Reply With Quote

3. Member 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);

}```  Reply With Quote

4. Senior Member Join Date
Dec 2008
Posts
526
Rep Power
0

## whay don't just use Math lib here?  Reply With Quote

5. Senior Member Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
12

## do you know 5 / 7 is 0
5 / 7.0 is 0.714....

for nextDenominator and nextNumerator, please declare them as double or float  Reply With Quote

6. Moderator   Join Date
Feb 2009
Location
New Zealand
Posts
4,716
Rep Power
18

## Java Code:
```half_pi *= termCount / termCount--;
half_pi *= termCount / termCount++;```
These lines may not be doing what you think. (Well, clearly they're not or you wouldn't have a problem ... .;-)

Try this:

Java Code:
```public static void main(String args[]) {
double foo = 10;
System.out.print(foo / foo--);
}```
Consult your textbook or whatever to see what the -- operator does.

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.  Reply With Quote

#### Posting Permissions

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