Like Tree1Likes
  • 1 Post By std10093

Help with calculating pi (beginner)

This is a discussion on Help with calculating pi (beginner) within the C++ Programming forums, part of the General Programming Boards category; I have only just begun programming and i am a bit stuck on a current logic problem. The program i ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10

    Help with calculating pi (beginner)

    I have only just begun programming and i am a bit stuck on a current logic problem. The program i made (which is very simple) for the purpose of calculating pi is based on the infinite alternating series:

    4/1 - 4/3 + 4/5 - 4/7 …

    I've already worked out the mathematics and it works, but it's very inefficient (since the input is the number of iterations). Obviously the series converges to pi as it goes towards infinity, but as there is no way to repeat a program an infinite number of times (without waiting an infinite amount of time) the technique i've employed is flawed. I was wondering if someone could give me a suggestion as to how i could make my program more efficient and get a more accurate reading of pi (without waiting a really long time for the answer).

    Thank you.

    The source code i have up until now is:

    Code:
    #include <iostream>
    #include <cmath>
    using namespace std;
    int main()
    {
      double pi, series, entry, number = 1, alternating_sign;
      int count;
      char ans;
    
    
    do
      {
      cout << "Entry:";
        cin >> entry;
    
    
        for (count = 0; count < entry; count ++)
        {
          alternating_sign = pow((-1.0), (count));
          series += alternating_sign / (number);
          number+=2;
         }
    
    
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(10);
    
    
      pi = 4*series;
    
    
      cout << "Pi equals " << pi << endl;
      cout << "Try again?\n";
      cin >> ans;
    
    
      } while (ans == 'Y' || ans == 'y');
    
    
      return 0;
    }

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,649
    Try this one.

    π˛/6 = 1/1˛ + 1/2˛ + 1/3˛ + 1/4˛ + 1/5˛ + 1/6˛ + 1/7˛ + 1/8˛ + ...

    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,649
    And here is the analysis I had made on a post that what discussing about this series I provided
    I calculated the absolute error of that(where x' is the machine number)
    x' = 3.141592645

    x= 3.141592653 (actual value)

    |ε| = |x - x'| = 0.000000008 = 0.08 * 10^(-7) < 0.5*10^(-7)
    which tell us that the machine value is going to be accurate at 7 decimal
    digits at the most.Here it is exact 7 digits.

    Then i calculated the absolute relative error

    |ρ| = |ε| / x = 0,025464790899483937645941521750815 * 10^(-7) =
    0,025464790899483937645941521750815 * 10^(-9) < 5 * 10^(-9)
    which says that at the most nine significant digits are going to be
    accurate.Here 8 digits are accurate.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10
    Thanks for the help, that really sounds much simpler . But im still worried about the iterations. I mean, wouldn't it have the same pitfall as it is increasing towards infinity. But i guess that really would depend on how many decimal points you intended to calculate the "true value" of pi.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Posts
    10
    You need to initialize the variable "series" to 0.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10
    to std10093
    I figured that i would probably would have to do that. But instead of placing a value exact to 8 digits i thought of making a program where the user types in the measured accuracy. I wanted to be able to input, let's say, a 20 digit measure; but i guess that would probably take a long time as well . Thanks again for your help i will try and make the changes to correct the program.

    By the way, do you know of any online or literary resource where i could obtain some further information on creating algorithms. The current textbook i'm using doesn't really have a lot of information on the development of logical sequences which is obviously crucial to programming.
    Thanks.

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10
    thanks fightmx, stupid error on my part. Although i guess i also aimed too high wanting to calculate any number of accuracy on my computer.

  8. #8
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,649
    Check out this formula then. Might help. By the way, you can see the code for the other formula here. If you google you may found more
    Salem likes this.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Glyfada,Athens
    Posts
    2,649
    Quote Originally Posted by JMaxwell View Post
    to std10093
    I figured that i would probably would have to do that. But instead of placing a value exact to 8 digits i thought of making a program where the user types in the measured accuracy. I wanted to be able to input, let's say, a 20 digit measure; but i guess that would probably take a long time as well . Thanks again for your help i will try and make the changes to correct the program.

    By the way, do you know of any online or literary resource where i could obtain some further information on creating algorithms. The current textbook i'm using doesn't really have a lot of information on the development of logical sequences which is obviously crucial to programming.
    Thanks.
    You can let him set the term to stop .

    I am not sure of what you are asking for. A great book for giving you an understand and make you think like a scientist in algorithms is the one of Cormen's. Google for it
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Feb 2013
    Location
    San Juan, PR
    Posts
    10
    Thanks a lot! I've barely started and i'm already trying to do things for myself . Being a musician for ten years i know what it means that practice is what it takes to progress in a given subject.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating MPG
    By Robot Kris in forum C Programming
    Replies: 7
    Last Post: 09-08-2010, 08:29 PM
  2. calculating the mean
    By bartleby84 in forum C Programming
    Replies: 9
    Last Post: 08-27-2007, 11:47 AM
  3. calculating e
    By warg in forum C Programming
    Replies: 14
    Last Post: 01-29-2006, 03:57 PM
  4. Help with calculating
    By Moffia in forum Windows Programming
    Replies: 3
    Last Post: 08-05-2005, 01:21 AM
  5. Windows programming for beginner (Absolute beginner)
    By WDT in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2004, 10:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21