Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

pi generation

by Pete_I (Beadle)
on Jan 29, 2006 at 03:09 UTC ( #526243=sourcecode: print w/ replies, xml ) Need Help??

Category: text processing
Author/Contact Info Pete_I
Description: calculates the digits of pi to $ARGV[0] accurately
#!/usr/bin/perl
###
#generate $ARGV[0] digits of pi.
#the algorithm is here:
#http://crd.lbl.gov/~dhbailey/
####
#written by Pete_I with help from BillN1VUX(from freenode)
####
#edited to make more efficient use of memory

use strict;
use warnings;
use Math::BigFloat;

my $DIGS = $ARGV[0] || 50;
Math::BigFloat->div_scale($DIGS+10);

my $pi = Math::BigFloat->new();
for(0 .. $DIGS) {
 $pi->badd( get_digit($_) );
}
print "\n", $pi->round($DIGS) . "\n";

sub get_digit {
 my $k = Math::BigFloat->new(shift);
 ( 1 / 16 ** $k ) *
 (
  (4 / (8 * $k + 1))-
  (2 / (8 * $k + 4))-
  (1 / (8 * $k + 5))-
  (1 / (8 * $k + 6))
 )
}

__DATA__
digits of pi to compare accuracy
3.14159265358979323846264338327950288419716939937510582097494459230781
+6406286208998628034825342117067982148086513282

Comment on pi generation
Download Code
Re: pi generation
by Bill_N1VUX (Initiate) on Feb 02, 2006 at 04:18 UTC
    Thanks Pete, that was a fun little project. I was happy to help since no one else on IRC seemed to notice your question and it interested me. It was a great experience for my first time on IRC. (I'd ignored IRC for longer than I ignored Perl 1..4. I'm not sure I have time to be a regular everywhere, but it was fun, so I'll be back.)

    My notes on how we fixed this program, and how it connects to other things, are on my journal http://use.perl.org/~n1vux/journal/28505.

    In order to link a comment on your posting to my journal entry, I decided it was time to do a properly signed Monk posting -- so I had to re-create my twice-zombied ID and actually log in, so my first IRC session begats my first monkish scriptorium.

    The whole Perl Monks theme amuses me in a nostalgic sort of way, since in the seventies, my main D&D characters ran a religious order for fun and profit.

    So thanks for the opportunity!

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://526243]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (16)
As of 2014-09-02 14:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (24 votes), past polls