Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Arithmetic-Geometric Mean calculation of pi.

by rpc (Monk)
on May 26, 2001 at 03:26 UTC ( [id://83450]=CUFP: print w/replies, xml ) Need Help??

This is an algorithm that converges very quickly on pi. Precision allowing, the number of 'good' pi digits doubles per iteration of the algorithm.

#!/usr/bin/perl use strict; use constant ITERS => 6; use Math::BigFloat; my $x = Math::BigFloat->new("2.0")->fsqrt; my $p = $x + 2; my $y = $x->fsqrt; for(1 .. ITERS) { my $xroot = $x->fsqrt; $x = ($xroot + 1/$xroot)/2; $xroot = $x->fsqrt; $p = $p * (($x + 1)/($y + 1)); $y = (($y * $xroot) + (1/$xroot))/($y + 1); print "$p\n\n"; }

Replies are listed 'Best First'.
Re: Arithmetic-Geometric Mean calculation of pi.
by quester (Vicar) on Jan 23, 2007 at 00:27 UTC
    I know this is ancient history now but...

    Use of Math::BigFloat without very carefully reading the documentation is considered harmful

    There is an old problem with the code and a new one.

    The old problem is that nothing is passed to Math::BigFloat to tell it how precise we want our answers. By default, Math::BigFloat->new("2.0")->fsqrt will return only 40 digits; see the discussion of div_scale in Math::BigFloat. This accounts for the problem that metadoktor noted. By setting the precision to -10000 I was able to calculate pi to 10,000 digits, and according to the Joy of Pi site, it is correct. (It took about 40 minutes on a fast Xeon.)

    The new problem is that every use of fsqrt needs to be changed from $x->fsqrt to ($x+0)->fsqrt. Apparently there has been a change to fsqrt in the last several years that causes $y = $x->fsqrt to change $x as well as $y to the square root of $x's original value. Most likely it is the change mentioned for bpow near the end of the Math::BigFloat documentation. Without the workaround the program converges quickly to 3.2693255..., which is high by over four percent... worse than slide rule accuracy.

    The revised code, set to ten thousand digits, is:

    #!/usr/bin/perl -w use strict; use warnings; use constant ITERS => 13; use Math::BigFloat; Math::BigFloat->precision(-10000); my $x = Math::BigFloat->new("2.0")->fsqrt; my $p = $x + 2; my $y = ($x+0)->fsqrt; for(1 .. ITERS) { my $xroot = ($x+0)->fsqrt; $x = ($xroot + 1/$xroot)/2; $xroot = ($x+0)->fsqrt; $p = $p * (($x + 1)/($y + 1)); $y = (($y * $xroot) + (1/$xroot))/($y + 1); print "Iter $_ $p\n\n"; }
Re: Arithmetic-Geometric Mean calculation of pi.
by metadoktor (Hermit) on Jan 02, 2002 at 19:55 UTC
    Your program calculates π as the following:

    3.1415926535 8979323846 2643383279 5028841974 186

    These are the actual digits of π:

    3.1415926535 8979323846 2643383279 5028841971 693

    Note that your program starts to calculate the digits incorrectly at the point where the digits are highlighted in bold.

    I double checked the digits of π by referring to my book The Mathematical Experience (pg 371) by P. Davis and R. Hersh and by using the online site www.joyofpi.com/pi.htm

    So either there is something wrong with your code/algorithm or Math::BigFloat does not accurately perform the desired operations.

    metadoktor

    "The doktor is in."

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://83450]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-03-29 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found