C for PI

This is a discussion on C for PI within the C Programming forums, part of the General Programming Boards category; I have been looking for a program to calculate pi to a digit given all I have seen so far ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    C for PI

    I have been looking for a program to calculate pi to a digit given
    all I have seen so far are
    100 digits
    1000 digits
    and
    1001 digits

    why can't there be one to display PI to any amount of digits?

    so I don't think it is possible for C basic C to compute pi.
    In calculus the equation (at least, my way of getting the equation is right)

    pi = 2 ( integral(sqrt(1-x^2)dx, from -1 to 1)

    I derived this 2 years ago, I think there is also a series to do this but that is basically it right there.

    If anyone finds a program to calculuate pi let me know. I would really appreciate it.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    update
    the expanded version of the computation of pi is
    arcsin(1)/2+x*sqrt(1-x^2)/2=PI/4
    or
    Code:
                      _____
    arcsin(1)      1\/1-1^2       PI
    ----------  +  --------  =   -----
        2              2           4
    
    so
     +-                    _____-+
     |  arcsin(1)      1\/1-1^2  |  
    4|  ----------  +  --------  |= PI
     |      2              2     | 
     +-                         -+
    Last edited by Lynux-Penguin; 04-28-2002 at 01:26 AM.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    WHAT THE ****
    WAIT!!!
    Code:
     +-         -+
     | arcsin(1) |
    4| --------- | = PI
     |     2     |
     +-         -+
    you can do that in C right?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    let me withdrawl the mathematical idiotism
    arcsin(1) = PI/2
    so
    2(arcsin(1)) = PI

    but arcsin in C uses the constant PI defined by math.h
    so it is back to the drawing board.

    hey, might as well right some math on CONES while I am at it.

    j/k
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    5 seconds on yahoo picked up hundreds of relevant and useful sites...ask me if you need help on the exact query terms.

    [edit] damn I always get beat making sarcastic posts [/edit]

  6. #6
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,493
    ditto

  7. #7
    Registered User sean345's Avatar
    Join Date
    Mar 2002
    Posts
    346
    pi = 2 ( integral(sqrt(1-x^2)dx, from -1 to 1)
    When I tried this it came out to close to but not exactly PI. If you want to go 100 or even 1000 digits then you will need something more acurate. This was wrong on the 7th digit.

    - Sean
    If cities were built like software is built, the first woodpecker to come along would level civilization.
    Black Frog Studios

  8. #8
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    but it can't be wrong because your taking the area of a circle who's radius is 1 which is PI

    The integral is for area under the curve and because
    R^2=Y^2+X^2 = Circle
    Y(x)=sqrt(1-x^2)
    integrate that and you get PI/2
    so 2 * that = PI exact
    but the problem is
    whenever you integrate on a machine the machine has a CONSTANT PI that is built in

    Yahoo eh?

    I use google
    http://www.google.com/search?hl=en&q...lating+pi+in+C
    and yes i have memorized the search patterns.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  9. #9
    Registered User toaster's Avatar
    Join Date
    Apr 2002
    Posts
    161
    uh...
    PI is circumference divided by diameter, I think.
    or
    const long double PI = 4 * arctan(1);
    const long double PI = 2 * arcsin(1);
    const long double PI = arccos(-1);

    // PI = 180 degrees (in terms of radians and degrees)

    I could be wrong, though.
    think only with code.
    write only with source.

  10. #10
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    From planet-source-code.

    Does this help??

    Code:
    //**************************************
    //INCLUDE files for :Precise Calculation of Pi
    //**************************************
    iostream.h
    math.h
    //**************************************
    // Name: Precise Calculation of Pi
    // Description:This code calculates an estimated value of Pi using the Leibnitz series (which is basically a power series expansion of a trigonometric function which allows to estimate Pi very well)
    // By: Eli
    //
    //
    // Inputs:n/a
    //
    // Returns:Prints the estimated value of Pi
    //
    //Assumes:n/a
    //
    //Side Effects:n/a
    //This code is copyrighted and has limited warranties.
    //Please see http://www.Planet-Source-Code.com/xq...s/ShowCode.htm
    //for details.
    //**************************************
    
    #include <iostream.h>
    #include <math.h>
    #define NUM_OF_ELEMENTS 20000
    int main()
    
    
        {
        double pi = 0;
        // Calculating pi/4
        for (long int n = 1; n <= NUM_OF_ELEMENTS; n++)
    
    
            {
            pi += (double) pow(-1, n+1)/(2*n-1);
        }
        // Calculating pi
        pi *= 4;
        cout << "Estimated PI value (" << NUM_OF_ELEMENTS << " elements of Leibnitz series): "<< pi;
        return 0;
    }
    Visit entropysink.com - It's what your PC is made for!

  11. #11
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    umm the last source code outputted:

    Estimated PI Value (20000 elements of Leibnitz Series): 3.14154

    I have more digits memorized
    3.14159265358979323...
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  12. #12
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    605
    LOL.... guess next time I should try b4 I post. Figured it meant 2000 places. Oh well.
    Visit entropysink.com - It's what your PC is made for!

  13. #13
    Registered User
    Join Date
    Apr 2002
    Posts
    200
    Why do you not just write your own program to do it? There are a plethora of sites with the necessary summations for you. Its a pretty trivial problem to implement them, and I'm sure that lots of people will be glad to help you if you have any difficulties. If all you want is the binaries for some reason, then you might as well go to one of the many websites that contain the value of pi to immense length.
    I go to encounter for the millionth time the reality of experience and to forge in the smithy of my soul the uncreated conscience of my race.

    Windows XP consists of 32 bit extensions and a graphical shell for a 16 bit patch to an 8 bit operating system originally coded for a 4 bit microprocessor, written by a 2 bit company, that can't stand 1 bit of competition.

  14. #14
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533
    I think the real problem is the memory allocation required for PI
    the last source can compute pi to quite some distance
    add a few zero's to the Constant and there you go
    BUT THE PROGRAM TAKES FOREVER IF YOU DO
    but it still only displays 3.14159
    because the allocation available for a double however if you debug it in Linux you can see the whole thing which is pretty accurate.
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary search on a HUGE file.
    By Appoloinus in forum C++ Programming
    Replies: 12
    Last Post: 03-03-2003, 12:29 AM

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