# Thread: pie calcualation project, almost done, just need some help

1. ## pie calcualation project, almost done, just need some help

hey whats up cboard, my teacher gave me a project to do. She wants us to write a program that will calculate this equation: pi *4*(1-1/3+1/5+1/7+1/9-1/11+1/13-1/(2*i)+1/(2*100+i))

she said to calculate for i = 100, 1000, and 100000

I have to use a while loop, and when i do it it works for 100, but for 1000 and 100000 it says inf, is there a way to fix this?

this is what i get in cmd:

PI1 = 5.0482e+060
PI2 = 1.#INF
PI3 = 1.#INF
Press any key to continue . . .

any help is appreciated, i can show my code if requested.

2. Go ahead and show the code.

gg

3. You're going to have to show your code. You say that it works for 100, but 5.0482e+60 is not particularly close to 3.14etcetera, having an error of ... well, a lot.

Do you know what the formula means (i.e., it alternates, and you have to run from i = 1 to 100 or whatever)?

4. Code:
```#include <iostream>
using namespace std;

int main (){

double pi1 = 3.1415;
double pi2 = 3.1415;
double pi3 = 3.1415;
double a = 0;
double b = 0;
double c = 0;
double i1 = 100;
double i2 = 1000;
double i3 = 100000;
double equation1 = 4*(1-1/3+1/5+1/7+1/9-1/11+1/13-1/(2*100)+1/(2*100+1));
double equation2 = 4*(1-1/3+1/5+1/7+1/9-1/11+1/13-1/(2*100)+1/(2*100+1));
double equation3 = 4*(1-1/3+1/5+1/7+1/9-1/11+1/13-1/(2*100)+1/(2*100+1));

while (a!=i1){
pi1 *= equation1 ;
a++;

}
while (b!=i2){
pi2 *= equation2 ;
b++;

}
while (c!=i3){
pi3 *= equation3 ;
c++;

}

double total1 = pi1;
double total2 = pi2;
double total3 = pi3;

cout << "PI1 = " << total1 << endl;
cout << "PI2 = " << total2 << endl;
cout << "PI3 = " << total3 << endl;

return 0;

}```

5. And now that I see the code, I see your steno error : pi equals 4*(1-1/3+1/5-1/7+ ... +1/(2i+1) + ...).

Your while loop should add up the alternating bits (the 1, 1/3, etc.) until i reaches the target number, then multiply by 4.

6. so what do i need to do/replace?

7. Originally Posted by pjr5043
so what do i need to do/replace?
Originally Posted by tabstop
Your while loop should add up the alternating bits (the 1, 1/3, etc.) until i reaches the target number, then multiply by 4.
You don't want any *= in your code, maybe +=.

8. PI1 = 403.142
PI2 = 4003.14
PI3 = 400003
Press any key to continue . .

alrite i tried the += .. this is what i got ^^
how can i find out if they are correct or not?

9. the correct value? you mean pi?
3.1415926?

10. no i mean for the 400, 4003, 400003 .. i can't tell if its doing teh calcualtions correctly

11. 1/11 will definitely not do what you intend - It is integer and therefor == 0

12. Your loop need to sum up the individual elements of that series. Start with a sum variable initialized to 0. Next, add 1/1 to that, then subtract 1/3, then add 1/5, etc... this is the part where your loop comes into play, the summation of these individual pieces. Once you've run your loop the desired number of times (100, 1000, 100000) then you multiply the sum by 4 which will then be your result. You need to figure out how to do that loop part. All of this should be in a function that you can call along with passing in a value (how many times you want the loop to iterate), then you can just call this function the three times with the various values (100, 1000, 100000) and have the function return this value which you then output for the user. As already hinted at, the summation/division part should not use integers but rather double since division of integer values is not going to give you correct values. The function should return a type double value, the result of the calculation, and accept an integer which is the number of times to do the loop part.

Originally Posted by pjr5043
i can't tell if its doing teh calcualtions correctly
You've been tasked to calculate the value of pi which is 3.14159... If you are getting anything that isn't remotely close to that then you are obviously not doing it correctly. 403.142 is nowhere near 3.14159...

Have you tried a search of the message board for this topic before posting?[/edit]

13. i guess i have to fix that equation, i know what pie is .. i have to find out this pie value using that equation, so i figured that would be a lot higher than pie

14. i guess i have to fix that equation, i know what pie is .. i have to find out this pie value using that equation, so i figured that would be a lot higher than pie
That is an approximation of pi using Taylor series. What I find suspect is "-1/(2*i)+1/(2*100+i)", but if you follow the pattern you will be fine.

15. Originally Posted by pjr5043
i guess i have to fix that equation, i know what pie is .. i have to find out this pie value using that equation, so i figured that would be a lot higher than pie
Actually, pie is something you eat, and has nothing to do with pi which is the ratio of the circumference of a circle to its diameter.
If you want to find pie, look in the supermarket!

What on earth made you think you should calculate pi by starting with a number very close to pi and constantly multiplying that by some other number? Surely you can see that all this does is increase that number quadratically? Even if it could work it would be cheating since you almost start with the answer anyway.