Page 3 of 3 FirstFirst 123
Results 21 to 28 of 28

Thread: c program to compute PI

  1. #21
    Join Date
    Mar 2011
    Beans
    31
    Distro
    Kubuntu 11.04 Natty Narwhal

    Re: c program to compute PI

    There's a simpler way.

    Still include math.h

    Code:
    double pi = 4.0 * atan( 1.0 );
    This works because the units are radians by default (2 radians = 360 degrees)
    The arctangent of one is 1/4 pi
    Just multiply 1/4 pi by 4 and you get pi ^.^

  2. #22
    Join Date
    Jun 2007
    Location
    Paraparaumu, New Zealand
    Beans
    Hidden!

    Re: c program to compute PI

    Quote Originally Posted by iamanidiot123 View Post
    There's a simpler way.

    Still include math.h

    Code:
    double pi = 4.0 * atan( 1.0 );
    This works because the units are radians by default (2 radians = 360 degrees)
    The arctangent of one is 1/4 pi
    Just multiply 1/4 pi by 4 and you get pi ^.^
    Yes, for odrinairy mortals set with a real-world problem to solve, that would be a good solution.

    As I understand it, this thread is about one way someone coming up with a working trigonometric function such as atan() might work it out.
    Forum DOs and DON'Ts
    Please use CODE tags
    Including your email address in a post is not recommended
    My Blog

  3. #23
    Join Date
    Apr 2012
    Location
    广州
    Beans
    231
    Distro
    Ubuntu Gnome 16.04 Xenial Xerus

    Re: c program to compute PI

    Yeah, there are dozen of way to solve PI, but compare to that method, if you want to get millions or billions of decimals, it's kind of slow. Because calculate atan itself may use Taylor expansion. its rate of convergence can not be as fast as that method. This is why it attracts me to try to find information about it.

    Pn = An - Bn = ( sqrt(An) + sqrt(Bn) )( sqrt(An) - sqrt(Bn) )
    Pn+1 = An+1 - Bn+1 = (An + Bn)/2 - sqrt ( An*Bn ) = 1/2 * ( sqrt(An) - sqrt(Bn) )^2

    When n->infinite, let's find
    lim (Pn+1)/ (Pn)^2 = lim 1/2 * 1 / ( sqrt(An) + sqrt(Bn) )^2 = lim (1/2) * 1/ 4*M (a, b) = 1/(8 M ( a, b ))
    So that it becomes a constant,
    Pn+1 ~ C* (Pn)^2
    Therefore, every iteration could produce double precision of decimal then last time ! which is really fast !

  4. #24
    Join Date
    Apr 2012
    Location
    广州
    Beans
    231
    Distro
    Ubuntu Gnome 16.04 Xenial Xerus

    Re: c program to compute PI

    Oh, I may forget it should use sqrt, which uses Newton's iterating theorem, it's slow too when the number of decimal has become skyrocketing, though it has double convergence. Maybe there are some other ways to speed up sqrt, but I don't know about that ...

  5. #25
    Join Date
    Mar 2010
    Location
    Dhaka, Bangladesh
    Beans
    210
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: c program to compute PI

    Quote Originally Posted by DaviesX View Post
    Oh, I may forget it should use sqrt, which uses Newton's iterating theorem, it's slow too when the number of decimal has become skyrocketing, though it has double convergence. Maybe there are some other ways to speed up sqrt, but I don't know about that ...
    Fast Fourier Transformation can be used, I could not understand the solution but it is available on internet. This problem is a good place to test how fast the convergence is and how many digits can be computed accurately within time limits.
    http://www.spoj.pl/problems/PIVAL/
    And as far as I know, many of the best solutions are implementations of FFT.

  6. #26
    Join Date
    Apr 2012
    Location
    广州
    Beans
    231
    Distro
    Ubuntu Gnome 16.04 Xenial Xerus

    Re: c program to compute PI

    Thanks for your post, but I don't think I can write a program that can compute millions of digits of PI, I think it's hard, will it ?
    I just know how to write a c program without making syntax error for the time being ...

  7. #27
    Join Date
    Oct 2011
    Location
    Chicago, IL
    Beans
    419
    Distro
    Xubuntu 10.04 Lucid Lynx

    Re: c program to compute PI

    Quote Originally Posted by DaviesX View Post
    Thanks for your post, but I don't think I can write a program that can compute millions of digits of PI, I think it's hard, will it ?
    I just know how to write a c program without making syntax error for the time being ...
    One of the easier problems holding you back from many digits will be floating-point precision. I think IEEE 754 only gives you about 50 bits of precison for 64-bit doubles, which is about 15 decimal digits. if you're using C99, you can get just over twice as many digits out of a long double.

    Check out GMP, which allows you to use arbitrary-precision numbers in C

    http://gmplib.org/

    I know this isn't the main question you're trying to answer, but this might be useful as you move forward.

  8. #28
    Join Date
    Apr 2012
    Location
    广州
    Beans
    231
    Distro
    Ubuntu Gnome 16.04 Xenial Xerus

    Re: c program to compute PI

    Thank you !

Page 3 of 3 FirstFirst 123

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •