[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

RE: [MacPerl] More investigation of pi



I went back and took a look at the Bailey-Bourwein-Plouffe pi algorithm,
which as far as I can tell does exactly what it says -- return the nth
arbitrary digit of the hexadecimal representation of pi. In Perl terms,
here's the program:

#!perl
my %list = (0 => 1);
my $pi = 0;

for $i (0..100)
{
$z = (8 * $i);
$a = (4 / (1 + $z));
$b = (2 / (4 + $z));
$c = (1 / (5 + $z));
$d = (1 / (6 + $z));

$digit = ($a - $b - $c - $d) / &powers_of_16($i);  
$pi += $digit;
printf("%3d      %.50f\n",$i,$pi);
}

sub powers_of_16
{
    my ($power) = @_;
    unless (defined($list{$power}))
      { $list{$power} = $list{$power - 1} * 16; }
    return $list{$power};
}

And this was the result:

  0      3.13333333333333330372738600999582558870315551757813
  1      3.14142246642246636412210136768408119678497314453125
  2      3.14158739034658163191693347471300512552261352539063
  3      3.14159245756743565891611069673672318458557128906250
  4      3.14159264546033645260081357264425605535507202148438
  5      3.14159265322808778364560566842555999755859375000000
  6      3.14159265357288086661924353393260389566421508789063
  7      3.14159265358897288322737040289212018251419067382813
  8      3.14159265358975225979065726278349757194519042968750
  9      3.14159265358979133964112406829372048377990722656250
 10      3.14159265358979311599796346854418516159057617187500
 11      3.14159265358979311599796346854418516159057617187500
 12      3.14159265358979311599796346854418516159057617187500
                           ^

So basically, I got better results with this formula in six iterations than
I did with Wallis' expansion with over 37 million! However, I also hit a
dead end at iteration 10, which gives the same result as using atan(1,1)*4.
Everything else down to 100 yields the same result, so I'm sure that's a
compiler limitation.

So, the question is, how would you-all recommend proceeding from there? Or
would you recommend it? ;) I ask the second question because I notice that
the expansion of 3.13+ breaks down at the point indicated by the carat, so
further expansion, if any, would depend on having a greater accuracy than 15
or 16 digits.

-- Creede


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org