Two questions in one (computing pi)

I finished a program recently that simply computes the value of pi up to about 5 decimal places. The program does what it's supposed to do....almost. However, after troubleshooting, I'm still left with one little problem and I guess a conceptual question.

First, after you run the program. The final computation is 3.14138...which is pretty close, but even after adding and subtracting another 1000 or so terms, you get the same answer. The actual number is supposed to be 3.14159. So I ask where do you think I went wrong?

Second, writing the nested loop confused the heck out of me. I then realized the problem was the way I initialized my nested for loop. Here's my final code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <cstdlib>
#include <iostream>

using namespace std;

int main()
{
    float sum;
    int i,j;

    sum = ((1.0/(2.0-1.0)) - (1.0/(2.0+1.0)));

    for(i=2; i<=10000; i++) {
        for(j=i+1; j%2!=0 && j<=10000; j++)
            sum += ((1.0/(2.0*i+1)) - (1.0/(2.0*j+1)));

        j++;
        i++;
    }

    cout << 4*sum << endl;

    system("PAUSE");
    return 0;
}


Now I ask, whats wrong about initializing line 14 to j=3 and then have the loop run? I would have thought 'j' would have kept incrementing but it turned out not to be so. I noticed 'j' stopped incrementing after it hit 5. And also, if there's a better way to code this in general I'd be happy for the advice. Thanks
Last edited on
If you have a link to the formula, I can check if it's implemented right, but first...try changing 'sum' from a float to a double, see if that helps.
the one in my practice book is...pi = 4(1-1/3+1/5-1/7+1/9 -...- 1/(2i-1) + 1/(2i+1))...and i actually did change float to double and that got me closer to the right answer...thanks alot!
I think the problem with j is that j=i+1 initializes it to something different each time the loop runs but j=3 doesn't.

BTW, just for fun, here's an interesting way to find pi: generator a large amount of random coordinates with x <= l and y <= l. Let l be an arbitrary number. count how many of those points are closer then l units. Pi= (number of points closer then l / total points) * 4.

-blueberry
ok i can give you a way to compute pi way more precisely and way more easy.
just do this:
pi=sin(0.00001)*18000000
and there you have pi.
or you could do:
1
2
3
4
5
6
//based on stirlingers approximation
int n=10000;int facult=1;float e=2.71828183f;
for (int i=1;i<=10000;i++) {
facult=facult*i;
}
float pi=pow(facult/pow((n/e),n),2)/(2*n);

if facult overflows then try using at least 1000
ok never mind about this one either... forgot c++ doesn't go higher then 2.147billion
Last edited on
@gelatine:

sin takes radians, so you would have to multiply by PI by 10,000!! :D

Or convert to radians first, which involves using PI !!

Any way this is getting away from the point of the assignment.
Last edited on
o damn supid radians lol ...
i was just letting him know that that isn't the only way of computing it.
Using a trig function to calculate pi is cheating becase they're based on a value for pi. You may as well just do arc cosine -1.

Back to the original algorithm, you're using this: http://en.wikipedia.org/wiki/Leibniz_formula_for_%CF%80
You don't need two loops, you need one loop that steps up by 2, and you need a multiplier that alternates between -1 and 1. You then just sum the series.
Sorry gelatine, am going to have another go!

forgot c++ doesn't go higher then 2.147billion


Were you thinking of the max size of an int? The max size for a double is somewhere around 1.7E308. The example was dealing with floats

It's alright, I've pulled a few clangers too, (probably much more than you :D) My mistakes have been partly due to staying up all night to 10am, and failing to read things properly. Cheers
thanks for the input, im learning alot lol...still troubled about my second question though.
Last edited on
You need a loop that is as follows:
1, -3, 5, -7, 9, -11, ...

Can you see how to generate that sequence?
to kbw,

yea i can code that, but my question is a bit more conceptual. What is it about the nested for loop that wont allow me to simply initialize 'j' as a number. So how come j=i+1 works but j=3 doesnt? You can see either way, 'j' will be assigned to the number 3 first.
You know that the j++ isn't in the second for loop, but in the first one, right? Your second for loop doesn't define its own scope, so it simply encompasses everything up to the semi-colon.
So if you initialize j=3, then you will run your loop, increment j, realize j%2 == 0, which will terminate the loop and then you add 1 to j, bringing it to 3. This will just repeat itself if you keep initializing j to 5.
If you use i it will change on every iteration of the outside loop.

EDIT: @Blueberry: That sounds quite interesting. I think I kind of get the connection with the radii, but I'll definitely have to work it out when I get home.
Last edited on
My point is you shouldn't have a second loop. I would expect you to have one loop.

The idea is you get closer approximations to pi as you add more terms in the sequence. Repeating the whole thing 10000 times with the i loop is pointless.

Try to use a single loop to generate the sequence I mentioned above. Those problems you mention about j will go away as they're not really relevant.
Last edited on
ah i get it now...i think that should do it..thanks to everyone
Topic archived. No new replies allowed.