Thread: value of pi

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    4

    value of pi

    is there not something in C++ that has the preset value of pi? like in the math.h file?

    Also does anyone remeber the formula for finding the value of a shpere and the area of a circle?

  2. #2
    Unregistered
    Guest
    Sphere V = (4/3)*R^3*PI
    Circle S = R^2*PI

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145

    Magos

    While speaking of PI...

    How do you calculate the actual value of PI? What algoritm do you use? It would be fun to make a program that calculates it (well, not with as many decimals as the super-computers do, but still...)
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  4. #4
    Registered User alex's Avatar
    Join Date
    Sep 2001
    Posts
    132
    The preset value of pi should be in math.h, and is called M_PI. For some reason I don't know, the microsoft version of math.h does not include this constant. You can use pi=4*atan(1).

    alex

  5. #5
    SOAK
    Guest
    #include <iostream.h>
    #include <stdio.h>
    #include <math.h>

    typedef unsigned char byte;

    int Drop();

    int iDigits,*piChain;
    byte *csDigits;

    void main()
    {
    cout<<"Number of Decimals to Calculate:\n";
    cin>>iDigits;
    cout<<"\n";
    ++iDigits;
    piChain=new int[iDigits*10/3+2];
    csDigits=new byte[iDigits+1];
    for(register int n=0;n<=iDigits*10/3+1;n++)
    piChain[n]=2;

    int lg=1;
    int m;
    for(n=0;n<iDigits;n++)
    {
    csDigits[n]=(byte)Drop();
    if(csDigits[n]==9)
    continue;
    if(csDigits[n]==10)
    {
    for(m=lg;m<n;m++)
    {
    if(csDigits[m]!=9)
    {
    csDigits[m]=1+csDigits[m];
    }
    else
    {
    csDigits[m]=0;
    }
    }
    csDigits[n]=0;
    lg=n-1;
    continue;
    }
    lg=n;
    }
    cout<<(int)csDigits[0]<<",\n";
    for(n=1;n<iDigits;n++)
    {
    cout<<(int)csDigits[n];
    }
    cout<<"\n";
    delete[] piChain;
    delete[] csDigits;
    }

    int Drop()
    {
    int r,q,iDrop;
    for(int m=0;m<=iDigits*10/3+1;m++)
    piChain[m]=piChain[m]*10;
    for(m=(iDigits*10/3+1);m>1;m--)
    {
    q=(int)floor(piChain[m]/(2*m-1));
    r=piChain[m]%(2*m-1);
    piChain[m]=r;
    piChain[m-1]+=(q*(m-1));
    }
    iDrop=(int)floor(piChain[m]/10);
    piChain[m]=piChain[m]%10;

    return iDrop;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 01:07 PM
  2. PI Equation in C
    By wallysworld in forum C Programming
    Replies: 13
    Last Post: 10-23-2006, 08:12 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