For Loop to calculate PI

Im trying to calculate the value of PI by using George's Series or the Leibniz Formula which is the sum of several values which alternate in adding and subtracting values. The goal of my program is to create for loops for the odd and then even values and then take the sum of those values.

This is what the series looks like for some reference,
pi/4=1-1/3+1/5-1/7+1/9-....

Here's my code: I keep receiving zero and I'm really stumped as to why, I could use any help!
int main()

{
double indval1,indval2,sumofiterations,total;
indval1=0;
indval2=0;


for (int i = 1; i < 500; i= i + 4)
{
indval1 = 1 / i;

for (int j=3; j<500; j=j+4)
{
indval2 =1/j;
}

}
sumofiterations=indval1-indval2;
total=4*sumofiterations;

cout << " " << total << endl;

return 0;

}
1 / i and 1/j are integer division, the result is an integer, any fractional part is discarded.

Use 1.0/i and 1.0/j instead , which will use type double for the calculation.
Right, that makes sense. Thank you!
Even with changing the 1 to 1.0, I'm still not getting anywhere near 3.14. Im confused where I'm messing up the calculations or initializations
But I missed some other issues. There are two loops, using i and j, but one seems to be nested inside the other, which looks wrong.

And, important, you need to add the result of each divide to the accumulator,
 
    indval1 = indval1  + 1.0 / i;
or more simply,
 
    indval1 += 1.0 / i;
okay, I've changed it as you suggested and I'm closer now but still not there. I received back a value of 3.13759. Should I increase the increment from 500 to something higher?

for (int i = 1; i <500; i= i + 4)
{
indval1 =indval1+ 1.0 / i;
}

for (int j=3; j<500; j=j+4)
{
indval2 =indval2+ 1.0/j;
}

sumofiterations=indval1-indval2;
total=4*sumofiterations;

It is an alternating series with diminishing terms, so (before multiplying by 4) the error should always be less than the magnitude of the first omitted term.

If you have got to j=500 then the error in the series sum could be as big as 0.002 and when you multiply by 4 that would potentially give an error of 0.008. Your answer is within the range of 3.14159... plus or minus 0.008, so OK.

In general, your error could be as large as 4/j, where j is the largest value that you get to. This series doesn't converge particularly quickly, I'm afraid.
Yes, my understand is that this approximation is relatively simple to code, but converges to a result verrry slowly.

Try increasing to 5000, then 50000 then 500000 iterations and so on. Of course it will slow down the processing. You may also reach the limit of the integer type. If that happens, try using unsigned long long int instead of int.
3.313759 is correct for the 500th iteration.

wikipedia wrote:
Leibniz's formula converges extremely slowly: it exhibits sublinear convergence. Calculating π to 10 correct decimal places using direct summation of the series requires about five billion terms


Here are the last 10 iterations:
1
2
3
4
5
6
7
8
9
10
481 3.14574
483 3.13746
485 3.14571
487 3.13749
489 3.14567
491 3.13753
493 3.14564
495 3.13756
497 3.14561
499 3.13759


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Topic archived. No new replies allowed.