The Museum of HP Calculators

HP Forum Archive 21

[ Return to Index | Top of Index ]

Calculating Pi
Message #1 Posted by Andrew Davie on 3 July 2012, 7:50 a.m.

I stumbled on a neat bit of C code when looking at the PB-2000C (a C-capable calculator, which is weird enough by itself). The code calculates Pi to any number of places. Here's it is...

long a=10000,b,c,d,e,*f,g; main(){printf("digits?");scanf("%ld",&c);c*=3.5;c-=c%14;f=malloc(4*c+4); for(;b-c;)f[b++]=a/5;for(;d=0,g=c*2;c-=14,printf("%.4d",e+d/a),e=d%a) for(b=c;d+=f[b]*a,f[b]=d%--g,d/=g--,--b;d*=b);}

Compiled and ran it on my machine. I don't understand how it works, but it does. Could anyone with a bit of math skills please comment on what's actually happening here? It looks like magic to me.

Thanks A

      
Re: Calculating Pi
Message #2 Posted by Egan Ford on 3 July 2012, 11:22 a.m.,
in response to message #1 by Andrew Davie

Quote:
It looks like magic to me.
Spigot (http://en.wikipedia.org/wiki/Spigot_algorithm).

Read Pi Unleashed. Ch. 6 for a detailed explanation.

For Unbounded Spigot checkout: http://www.comlab.ox.ac.uk/oucl/work/jeremy.gibbons/publications/spigot.pdf

A C version of the Unbounded Spigot: http://sense.net/~egan/hpgcc/#Unbounded%20Spigot%20Algorithm

Edited: 3 July 2012, 11:30 a.m.

            
Re: Calculating Pi
Message #3 Posted by Andrew Davie on 3 July 2012, 11:55 a.m.,
in response to message #2 by Egan Ford

.... wow. Thanks!

Edited: 3 July 2012, 4:55 p.m. after one or more responses were posted

            
Re: Calculating Pi [OT]
Message #4 Posted by Tim Wessman on 3 July 2012, 3:23 p.m.,
in response to message #2 by Egan Ford

Side note - libtommath is in use in the 39gII.

I've never found any good speed comparision between that and other (free/commercial) libraries. Anyone seen one before anywhere?

TW

Edited: 3 July 2012, 3:24 p.m.

                  
Re: Calculating Pi [OT]
Message #5 Posted by Gjermund Skailand on 4 July 2012, 6:33 a.m.,
in response to message #4 by Tim Wessman

I have a fairly comprehensive beta version of a longfloat library written in hpgcc3 for hp50g. It is user accessible, and it is based on decnumber. I'm fairly confident it will outpermform similar librarys for any hpcalculator. Send me a note if you want to give it a try. Best regards Gjermund

                        
Re: Calculating Pi [OT]
Message #6 Posted by Egan Ford on 4 July 2012, 11:21 a.m.,
in response to message #5 by Gjermund Skailand

Quote:
I'm fairly confident it will outpermform similar librarys for any hpcalculator.
Agreed for FP, but not for INT. libtommath is much faster for large integer math. At least based on the comparisons I did years ago.
                              
Re: Calculating Pi [OT]
Message #7 Posted by Gjermund Skailand on 6 July 2012, 3:30 a.m.,
in response to message #6 by Egan Ford

Hi again,
I agree, there are (much) faster librarys for special purposes.
My library is suitable for floating point evaluation up to about 1000 digits for real and complex values.
It is accessible from user-rpl, you set number of floating point digits, and can directly evaluate expresions like e.g.
'4*ATAN(1)' ->FNUM
100 digits in 0,1s , 200 digits in 0,4s, 500 digits in 4,1s
'SQRT(-2)' ->FNUM
1000 digits in 0,1s, 10000 digits 11,2s
except for basic arithmetic there is a limit of about 1000 DIGITS
Best regards
Gjermund


[ Return to Index | Top of Index ]

Go back to the main exhibit hall