Thread: looking for quick method to calculate pi

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    4

    looking for quick method to calculate pi

    I'm looking for a FAST way to calculate X digits of pi in hexadecimal without using any external libraries (i.e. APFloat, etc.).

    Currently I've found code for extracting any specific digit of pi in hexadecimal (using the BBP formula, I believe), but using this code to compute even 1000 digits of pi is SLOW.

    Thanks in advance.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I wonder why.

  3. #3
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    You're going to have to keep wondering, because it's not homework. (I assume that's what you're thinking, anyway.)

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > using this code to compute even 1000 digits of pi is SLOW.
    Well pi is an extremely long real number that has no documented repetitions for about 50 billion places, if I remember correctly. Few of us are mathematicians; I would be happy with what is already possible through the work of someone else.

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    As Citizen said.

    There was this mathematician that took 15 years to calculate manually the first 1000 digits, if I recall correctly. He pusblished the results and died peacefully, and an acomplished man, in his bed (for his own sake, I hope not alone), only to be found later on he made a mistake somewhere halfway. And all numbers that followed were wrong.

    Anyways... You can use the Machin's Formula
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You could also go to one of the countless sites that show PI at a high precision, copy that into a file, and read the file.
    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

  7. #7
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    would it be faster or easier to calculate the square root of 2 to X hexadecimal digits?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    A close approximation of pi is the quotient of 22 / 7. You may have an easier time converting that to hexidecimal.

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by citizen
    A close approximation of pi is the quotient of 22 / 7. You may have an easier time converting that to hexidecimal.
    No,
    pi = 3.141592...
    22/7 = 3.142857...

    As you can see the approximation is not very close, although in practice this kind of precision would be enough in most cases.

    I know there's a method to calculate the n-th hex digit of pi (I think even not knowing about the other heximal places) and you could google for that. Probably, you could correctly calculate pi to any digit in a loop.

    It was too advanced mathematics for me to understand...

  10. #10
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273

  11. #11
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Here's how I calculate pi:
    Code:
    double x = 4 * atan( 1.0 );
    Gotta love the "please fix this for me, but I'm not going to tell you which functions we're allowed to use" posts.
    It's like teaching people to walk by first breaking their legs - muppet teachers! - Salem

  12. #12
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398
    Quote Originally Posted by Micko
    Here's how I calculate pi:
    Code:
    double x = 4 * atan( 1.0 );
    A fine technique, as long as you understand and accept the limitations of atan().

  13. #13
    Registered User
    Join Date
    Jul 2006
    Posts
    4
    The problem is that want the *digits* of pi (or the sqrt of 2), not the value.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Once you have the value you can get the digits, but yes, I think reading pre-calculated values from file may be easier.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Enjoy: pi digits
    Seems correct to me.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. on method pointers and inheritance
    By BrownB in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2009, 07:50 PM
  2. Pi - Hm, somethign is not right here.
    By MadnessRed in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2008, 01:07 PM
  3. Small program that has to calculate miles per gallon
    By Guti14 in forum C++ Programming
    Replies: 6
    Last Post: 01-06-2004, 02:47 PM
  4. Program to Calculate Pi
    By zowen in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2003, 11:18 AM
  5. C for PI
    By Lynux-Penguin in forum C Programming
    Replies: 13
    Last Post: 04-28-2002, 07:37 PM