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

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    37

    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. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Go ahead and show the code.

    gg

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    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. #6
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    so what do i need to do/replace?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pjr5043 View Post
    so what do i need to do/replace?
    Quote 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. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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. #9
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,229
    the correct value? you mean pi?
    3.1415926?

  10. #10
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    no i mean for the 400, 4003, 400003 .. i can't tell if its doing teh calcualtions correctly

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1/11 will definitely not do what you intend - It is integer and therefor == 0
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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.

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

    [edit]Have you tried a search of the message board for this topic before posting?[/edit]
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  13. #13
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    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. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by pjr5043 View Post
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM