Thread: pi spigot program

  1. #1
    Registered User
    Join Date
    Apr 2006
    Location
    Encinitas, CA 92024
    Posts
    11

    pi spigot program

    I am attempting to use the Bailey-Borwein-Plouffe algorithm, ((4/(8n+1))-(2/(8n+4))-(1/(8n+5))-(1/(8n+6)))/16^n for the nth digit of pi.

    Here is my code:
    Code:
    #include <stdio.h>
    int main(void){
      float n, pi_digit, hex_base, pi;
      for(n=0, hex_base=1; n<=20; n++) {
          pi_digit = 4 /(8 * n + 1);
          pi_digit -= 2 / (8 * n + 4);
          pi_digit -= 1 / (8 * n + 5);
          pi_digit -= 1 / (8 * n + 6);
          pi_digit /= hex_base;
          hex_base *= 16;
          printf("%x ",pi_digit);
          }
      getchar ();
    }
    I'm getting the wrong answer and I don't know if I'm handling the hexadecimal conversion correctly.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    First of all, use doubles instead of floats for greater precision.

    I'm getting the wrong answer
    Completely wrong?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Why are you printing the digit as a hex value? I've never seen PI represented in hex...

    Since pi_digit is a float you have to use %f or one of the other floating point conversion specifiers. %x is used for printing integers.
    Last edited by itsme86; 07-14-2006 at 03:38 PM.
    If you understand what you're doing, you're not learning anything.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Google "Bailey-Borwein-Plouffe algorithm hexadecimal" gave me this, which gives the reason:
    The kicker here though is that a single digit of the transcendent number is extracted, and as the (1/16)^n term indicates, that digit is in hexadecimal. Other algorithms must be used to convert the partial series of hexadecimal digits into decimal, or base 10.
    [edit]
    Edit for edit: Since the digit created is hexadecimal, you don't want %f. (I don't think hexadecimal numbers can be floating-point, can they?)
    [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    [edit]
    Edit for edit: Since the digit created is hexadecimal, you don't want %f. (I don't think hexadecimal numbers can be floating-point, can they?)
    [/edit]
    Floating point digets are internally base two, so printing them out as base 16 should be even simpler then printing them out as base 10.

    That said, There are no standard function that prints hexadecimal floating point numbers that I know of. You would have to either find a librairy, or write your own (which is not too hard).
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Location
    Encinitas, CA 92024
    Posts
    11
    This code from the Internet works, but I did not write it:

    Code:
    #include <stdio.h>
    int a[52514], b, c=52514, d, e, f=1e4, g, h;
    main(){
      for(;b=c-=14;h=printf("%04d",e+d/f))
      for(e=d%=f;g=--b*2;d/=g)
        d=d*b+f*(h?a[b]:f/5),a[b]=d%--g;
      getchar();
    }

  7. #7
    Registered User
    Join Date
    Apr 2006
    Location
    Encinitas, CA 92024
    Posts
    11
    Yes, completely wrong--I don't even get the initial 3. Yes, pi can be written out in hexadecimal digits. I expected everyone here to be familiar with BPP's formula for the nth digit of pi, as it is the only one I know of that allows for individual digits to be calculated.

  8. #8
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by eehiram
    I expected everyone here to be familiar with BPP's formula for the nth digit of pi, as it is the only one I know of that allows for individual digits to be calculated.
    Perhaps, but this is a C forum, not a math forum
    If you understand what you're doing, you're not learning anything.

  9. #9
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    http://cboard.cprogramming.com/showthread.php?t=80978

    Hmmm. Looks strangely familiar eh? Either you are cross-posting and trying to be uber elite by changing your name, or you guys are in the same class.

    Either way, there are tons of sites on the net about this very thing.

  10. #10
    Registered User
    Join Date
    Apr 2006
    Location
    Encinitas, CA 92024
    Posts
    11
    Sorry if you got the wrong impression. I meant that all you guys here seem like you're really hard core and everything is easy to you, and calculating pi with BPP seems like it would be a popular thread.

    In regards to the other thread, I have no connection to it whatsoever. I simply wanted to calculate a couple million digits of pi and BPP seems like it would allow me to turn the computer off and on without having to start over from the first iteration each time.

    But I'm going to switch to something else and calculate a lot less digits. Hexadecimal calculations don't seem to be working out, even though I know the CPU uses them in machine code.

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    even though I know the CPU uses them in machine code.
    There seems to be confusion here. The CPU is a binary beast. It just sees 0s and 1s. Hexadecimal is just a convenient way for humans to look at those 0s and 1s. If you wanted to you could look at them in decimal, but it's not as simple to convert.

    One hexadecimal digit represents exactly 4 binary digits so it's very easy for humans to translate between the two. If you see the hexadecimal number 0xF0 then you can tell right away that it's 11110000 in binary. It's not so easy to tell when you see the decimal representation 240.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  2. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  3. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  4. Program that calculates the value of PI
    By noodles in forum C Programming
    Replies: 7
    Last Post: 09-06-2006, 05:20 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM