Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

n1vux (1492)

n1vux
  (email not shown publicly)
http://boston.pm ... x.cgi?BillRicker
AOL IM: n1vux (Add Buddy, Send Message)
Yahoo! ID: n1vux (Add User, Send Message)

Only started with Perl4 and Perl5 in 1995. I was doing AWK etc for 12 years before that, and resisted switching. I've been doing OO since before C++ hit bigtime, with Objective-C and SmallTalk, so I really like the (no longer new) Perl5 OO style; and the Lispish Map style is also an old friend. What do I hack with Perl? All data that passes my way; systems monitoring scripts at $DayJob, weather data at night, and I cheat on NPR word puzzles. Member: Boston.pm.org [pm.org] BLU.org [blu.org] /. LinkedIn [linkedin.com]

N1VUX is my FCC-issued ham radio callsign.

Journal of n1vux (1492)

Wednesday January 31, 2007
11:24 PM

More fun with Perl and Pi

[ #32292 ]
A year ago #28505, I answered a question on Perl Monks about calculating Pi with Math::BigFloat. Today the always funny XKCD made me go back and look at that program, as I had two rather less useful approximations of Pi to calculate now.

I'm somewhat surprised that Math::BigFloat doesn't have an AntiLog function for Euler's base 'e' (natural exponent). Hard coding it as a constant isn't terrible, but not nice either. I suppose I could compute it too ...

#!/usr/local/bin/perl

###
# Adaptation of my prior program to test XKCD's formulas 2007-01-31
# (9^2 + 19^2/22)^(1/4) = PI
# (e^pi - pi) = 20 - delta
# http://xkcd.com/c217.html
####

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

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

# (9^2 + 19^2/22) = PI
print "(9^2 + 19^2/22) = PI ? \n";
my $pi = new Math::BigFloat '9';
$pi->bpow(2);
$pi->bpow(2);

my $term= new Math::BigFloat'19';
$pi->badd($term->bpow(2)->bdiv(22));

# $pi->bsqrt()->bsqrt();
$pi->broot(4);

# compare to known-good from bottom of file
my $PI=(new Math::BigFloat <DATA>);
my $good=$PI->copy()->round($DIGS+2);
print $good, "\n";
print $pi->round($DIGS+1), "\n";
print (   ($good - $pi), "\n");

#####################
# part 2
# (e^pi - pi) = 20 - delta
print " (e^pi - pi) = 20 ?\n";
$pi=$PI->copy();
# my $e = (new Math::BigFloat '10')->bpow((new Math::BigFloat 1)/(new Math::BigFloat $LOG_10));
my $E = new Math::BigFloat '2.71828_18284_59045_23536';
my $VENTE=new Math::BigFloat '20.0';
print "$VENTE \n";
my $diff = $E->copy()->bpow($PI)->bsub($PI);
print $diff->round($DIGS)," \n";
print " ",(($VENTE - $diff)->round(3)), "\n";

# some accurate pi to compare output to:
__DATA__
3.141592653589793238462643383279502884197169399375105820974944592 30781640628621

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • $pi->bpow(2); is duplicated.
  • I came up with the following code recently as an experiment:


    use strict;
    use Math::BigFloat;
    use Time::HiRes;

    my $x = Math::BigFloat->new(my $str);
    $x->accuracy(100);
    my $start = (times)[0];
    $x=48*atan2(1,18)+32*atan2(1,57)-20*atan2(1,239);
    my $end = (times)[0];
    my $elapsed = $end-$start;
    printf "%.100f".$x."\n";
    printf "that took %.100f CPU seconds.\n",$elapsed;

    My source for the calculation of pi is a formula attributed to Gauss in the 1977 Van Nostrand Reinhold "Encyclopedia of Mathematics." I was interested