Thread: Pi - Hm, somethign is not right here.

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    10

    Pi - Hm, somethign is not right here.

    ok, here is my formula

    PHP Code:
    [CODE]/* Pi loops, we how many times??? */
    while ( cycles loops ) { // While x is less than 10 
        
    cycles += 1;             /* 1 loop started. */
        
    pi += (plus * (/ ((cycles) + 1)));
        
    plus = (plus);
        
    /* tempPi = ((1 - pi) * 4); */
        /* system("cls"); */
        /* progress = ((cycles / loops) * 100) */
    ;
        
    printf("%.0f / %.0f\n",cycles,loops);
    }
    /* Done, end of loading */
    system("cls");

    /* Calculate pi (1)*/
    pi = (* (pi));[/CODE
    I am using this formula

    http://upload.wikimedia.org/math/9/e...d1d52205a9.png

    ---

    I have several problems.

    The main one
    1 - Can I increase the number of decimal places - I am using float ( I get 3.141637086828610000000000000000000000000000000000 ?

    2 - Why does it not give me the answer I expect - (I expect 3.14159265358...) (I tried with 2 million cycles).
    - probably a bad wording, what is meant here is a question about how c++ works. Is there a limit to the decimals place during a calculation?

    3 - math based, is there a better formula that i can use? I know there are other forumlas around but this one seems to be the one they recommend for programming.

    ---

    This is not homework as such, I am not wanting someone to re-write it for me with a better formula and code as I am trying to do this to learn. Because it is not homework, I am willing to change to another language if it is free, similar syntax and is not too hard to learn.

    ---

    Thanks

    Anthony
    Last edited by MadnessRed; 09-11-2008 at 12:14 PM.

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The answers to all three lie in the way floating point numbers work. I suggest looking them up in Wikipedia.

    And yes, there are better ways to get PI, since the algorithm you have suffers greatly from the inaccuracy problem.

    For example, many compilers have the constant M_PI in <math.h>. Just use that. Failing that, define PI as your own constant. Look the value up on some website.

    Simply put, there's no good reason to ever compute PI yourself. It's a fundamental geometrical constant. It's not ever going to change as long as we perceive the world as an Euclidean, 3-dimensional space.
    Last edited by CornedBee; 09-11-2008 at 12:16 PM.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I believe it to be due to floating point inconsistencies (a floating point isn't accurately stored).
    If you need better and/or more precision, try using a double or long double.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Oh, and you could get integer math issues in this expression:
    Code:
    (1 / ((2 * cycles) + 1))
    If cycles is an integer type, this will always evaluate to 0.

    Edit: I see it is not. Never mind.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Aug 2008
    Posts
    10
    I am calculating pi because I am trying to learn c++. And because I want to see the formulas work.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    10
    ok, I have edited the code a bit myself now.

    PHP Code:
    [CODE]
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>

    int main()
    {
    double pi,a,b,c,x,a1,b1,c1,x1;
    float loops,cycles;

    /* Well, done no clyles yet. */
    cycles 0;

    /* Title */
    printf("Pi\nBy Anthony\n");

    /* Get the number of loops */
    printf("Enter number of loops: ");
    scanf("%f", &loops);

    /* We are doing something... */
    printf("Calculating Pi...\nPlease Wait");

    /* Now lets set the first values. */
    1;
    = (1/pow(2,0.5));
    0.25;
    1;
    /* And then set last values to that... */
    a1 a;
    b1 b;
    c1 c;
    x1 x;

    /* Pi loops, we do it how how many times??? */
    while ( cycles loops ) {
        
    cycles += 1;             /* 1 loop started. */
        /*start the calculations */
        
    = ((a1 b1) / 2);
        
    pow((a1 b1),0.5);
        
    c1 x1 pow((a1),2);
        
    = (x1);

        
    /* Set a1,b1,c1 and x1 ... */
        
    a1 a;
        
    b1 b;
        
    c1 c;
        
    x1 x;
        
        
    /* show progress */
        
    printf("%.0f / %.0f\n",cycles,loops);
    }
    /* Done, end of loading */
    system("cls");

    /* Calculate pi */
    pi = (pow((b),2) / (c));

    /* Output */
    printf("Done. Pi is %.25f\n",pi);
    system("pause");
    }
    [/
    CODE
    My question is, how can I get more than 15 decimals places?
    Last edited by MadnessRed; 09-12-2008 at 11:04 AM.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You could use a library like GMP that gives you arbitrary-precision numbers.
    http://gmplib.org/pi-with-gmp.html
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    The larch
    Join Date
    May 2006
    Posts
    3,573
    There should also be algorithms to compute Nth digit of pi, so you wouldn't need anything more than built-in types.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Calculating digits of pi hardly seems like a logical choice of programs to write in order to learn a language. You should be aware of the computational complexity of many of the algorithms used to calculate digits of pi (at least the ones which calculate more than one digit per pass). Why not just write a calculator or something... If you were learning F or something, I could understand this being a logical program to write. I guess I am not here to judge.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. PI Equation in C
    By wallysworld in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 08:12 PM
  2. looking for quick method to calculate pi
    By MindlessXD in forum C++ Programming
    Replies: 16
    Last Post: 07-19-2006, 03:59 PM
  3. Pi Calculation
    By EvilGuru in forum C Programming
    Replies: 2
    Last Post: 05-02-2005, 04:25 AM
  4. Pi and the standard library
    By FOOTOO in forum C Programming
    Replies: 7
    Last Post: 04-15-2005, 11:23 AM
  5. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 07:37 PM