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

  1. #16
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    pjr5043,

    This is not a simple formula that you can plug into your program, but rather an infinite series. (To be honest, I don't understand the notation your teacher gave you.) You can't make a calculation with an infinite number of elements, but with a computer, you can easily carry this series out to 100 or 100,000 elements.

    Here is a reference.

    The series is equal to one-quarter of PI, and it looks like this:
    1 - 1/3 + 1/5 - 1/7 + 1/9 -1/13 +1/17...

    Can you see what's next? ...-1/19 +1/21 -1/23...

    You need to write a loop does this automatically for 100, 1,000 or 100,000 elements. Each time you go through the loop, you will be adding-and-subtracting smaller-and-smaller fractions. In other words, your program will be figuring-out the fractions... For example, you won't see "1\13" or any of those fractions anywhere in your C++ code... You might see "1/i". Does that make sense?

    I assume you know how to write a loop that increments a number, or adds 5 to a number every time through the loop... If you don't know how to do that, you need to re-study loops before you jump into this project.

  2. #17
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by iMalc View Post
    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.
    I think the equation was written wrong. It should be:

    pi = 4 * (1 - 1/3 + 1/5 - 1/7...)

    (Which is true, by the way)

    And it looks like an assignment, not something that was thought up.

  3. #18
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Make Pi a define(ed) value in your program, and then don't touch it until right at the end when you compare your value for Pi, to the real (close approximation) of Pi. You know your equation is goofed, I believe. You should be having - then a +, then another - and +, throughout the series, iirc.

    All the rest of your program except one line of the series equation, is rubbish - junk it.

    Use the while loop to work with the digits after the decimal place, and only do the multiplication, (* 4), right at the end.
    Last edited by Adak; 02-15-2008 at 02:02 AM.

  4. #19
    Banned
    Join Date
    Nov 2007
    Posts
    678

    also this is very inaccurate method

    very naive method of finding pi
    see my output . . .
    Code:
    >>> mpi(10)
    pi 3.04183961893
    >>> mpi(100)
    pi 3.13159290356
    >>> mpi(1000)
    pi 3.14059265384
    >>> mpi(10000)
    pi 3.14149265359
    >>> mpi(100000)
    pi 3.14158265359
    >>> mpi(1000000)
    pi 3.14159165359
    >>> mpi(10000000)
    pi 3.14159255359
    >>>

  5. #20
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    It's slow as a dog, but if you carry the arithmetic out far enough, it does find the correct digits.

    It's just S-L-O-W as a snail if you want a lot of digits, compared to many other algorithms to find Pi.

  6. #21
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    pjr5043,

    I had another idea that might help you through this...

    Start with a program that simply "calculates" 1/3 by using an infinite series in a loop. As you know, 1/3 is represented in decimal by the infinite series 0.3333... So, the series looks like this:
    3/10 + 3/100 + 3/1,000 + 3/10,000... (Or, like this: 0.3 + 0.03 + 0.003 + 0.0003... But, you should use fractions... I should say division... in your program.)

    The first time though the loop, you will get 0.3*. The 5th time through the loop you will get 0.33333*, etc.

    Once you get that working, you can modify the code inside you loop to calculate the Taylor series.


    * You won't get these exact numbers, due to the way floats and doubles are stored in binary. So, don't worry if you see something like 0.33299999999999 instead of 0.333.
    Last edited by DougDbug; 02-15-2008 at 04:37 PM.

  7. #22
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    4*(1- 1/2i-1 + 1/2i+1) .. thats the equation, i just need to figure out how to implement it.. this stuff is pretty hard

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pjr5043 View Post
    4*(1- 1/2i-1 + 1/2i+1) .. thats the equation, i just need to figure out how to implement it.. this stuff is pretty hard
    That is by no means the equation. The equation is: pi = 4*(1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + (-1)^i/(2*i+1) + ...). In summation notation, the right hand side is (bad ASCII art alert):
    Code:
       inf
      -----
      \      (-1)^i
    4* >     ------
      /       2i+1
      -----
       i=0
    Summations are trivially easy to code up using for-loops (start with the sum equal to 0; each time through the loop add up the bit off to the right). Multiplication by 4 is not that hard either. Of course, we won't be able to stop the loop at infinity; we'll have to settle for the first hundred (or thousand, etc.) terms as given in your assignment.

  9. #24
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    alrite guys, i'm super confused with all of this right now.

    This is what my teacher told me today:
    She said that this "4*(1- 1/2i-1 + 1/2i+1)" was the equation i needed to use. She then said that this equation needed to go to a loop and add by increments of 2 each time, so i figured make a variable like c, then do c+2. I really can't get it to work.

    To be honest, if someone on here can just give me an equation to use, i'll paypal you $5, that's how bad i need this right now, i have so much other work to do, and i'm just never going to understand this project.


    This is the exact description i was given:

    Description : You can approximate π by using the following series :
    π = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ........ - 1/(2i - 1) + 1/(2i + 1) )

    Write a program that displays the π value for i = 100, 1000 and 100000

    note : find the pattern in the series.

  10. #25
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    We've put the equation up here six times, and you just posted it yourself. Note how this:
    4*(1- 1/2i-1 + 1/2i+1)
    and this
    π = 4 * (1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 - ........ - 1/(2i - 1) + 1/(2i + 1) )
    are not in fact the same thing, which is what we've been trying to tell you all along.

    One last time:
    when i is 1: 1/(2i-1) - 1/(2i+1) = 1 - 1/3
    when i is 3: 1/(2i-1) - 1/(2i+1) = 1/5 - 1/7
    when i is 5: 1/(2i-1) - 1/(2i+1) = 1/9 - 1/11

    So to get 1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + ... + 1/(2i-1) - 1/(2i+1) you should do ..............

  11. #26
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    i would need to have i equal all numbers from 1 to 100? than 1000 and 100000?

  12. #27
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pjr5043 View Post
    i would need to have i equal all numbers from 1 to 100? than 1000 and 100000?
    If you're going to do two at a time (with a + and a -), then you have to go by twos. You can let i be every number, but then you'll have to take care of the alternating + and - yourself (with sign = -1*sign or whatever).

  13. #28
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    alrite, so i can put like int i+2 in the loop right? .. but then what should my command line be for the loop .. right now i have c!=100 is that right?

  14. #29
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by pjr5043 View Post
    alrite, so i can put like int i+2 in the loop right? .. but then what should my command line be for the loop .. right now i have c!=100 is that right?
    No and no. If you're doing a for loop you would need to
    (1) start with i = 1
    (2) keep going unless i exceeds 100
    (3) and add 2 to i each time

  15. #30
    Registered User
    Join Date
    Jan 2008
    Posts
    37
    i really don't know, i'm never going to get this right because my teacher is a terrible at teaching, i already submitted my file, but honestly i'm being straight with you, if one of you were to just tweak my original code to make this thing work, i could see what you did, and that is the best way i learn things, i'm absolutely horrible and things like this right now, i need to see what i did and what i should of did, thats how my brain works

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