# pi spigot program

• 07-14-2006
eehiram
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.
• 07-14-2006
dwks
First of all, use doubles instead of floats for greater precision.

Quote:

Completely wrong?
• 07-14-2006
itsme86
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.
• 07-14-2006
dwks
Quote:

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 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]
• 07-14-2006
King Mir
Quote:

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).
• 07-14-2006
eehiram
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(); }```
• 07-14-2006
eehiram
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.
• 07-14-2006
itsme86
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 ;)
• 07-14-2006
VirtualAce
http://cboard.cprogramming.com/cplusplus-programming/80978-looking-quick-method-calculate-pi.html

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.

• 07-15-2006
eehiram
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.
• 07-15-2006
itsme86
Quote:

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.