i have a question about this, if anyone wants to find the time. How many decimal places of accuracy will perl allow? This algorithm gets more accurate after every iteration, but perl stops at about 10 or 11 places of accuracy and the rest is off, which makes me wonder. It's not obfuscated except for the algorithm...
#!/usr/local/bin/perl -w use strict; my($s,$a)=(5*(-2+sqrt 5),.5);sub a{5/(pop)-1} sub b{(a(pop)-1)**2+7}sub c{my$s=pop;(a($s)/2 *(b($s)+sqrt(b( $s )**2-4*a($s)**3)))**(1/5)} sub d{my$s=pop;25/((c($s)+a($s)/c($s)+1)**2*$ s)}sub e{my ($s,$a,$m)= @_;($s**2*$ a)-5**$m*(( ($s**2-5)/2 )+sqrt$s*($ s**2-(2*$s) +5))}my($x, $n)= (shift ||10,0);do{ $a=e($s,$a, $n);$s=d($s )}while$n++ <$x;print"" ,(1/$a),$/;
jynx

Replies are listed 'Best First'.
Re: tasty pi
by arhuman (Vicar) on Jul 07, 2001 at 13:49 UTC
    I may be wrong but Perl double are based on C double which are defined like this according to ANSI/ISO IEEE 754-1985 standard :

    double -> 64 bits
    A double element has a sign bit, an 11-bit exponent and a 52-bit fraction.
    The precise form to be used is quite tightly specified by the standard IEEE 754 which is implemented in hardware on most modern machines.
    Features of the notation:
    The first bit represents the sign of the number.

    The next 8 bits (float or single precision) or 11 bits (double, double precision)
    represent the exponent in excess-127 or excess-1023 notation.
    To overcome difficulties related to signed exponents, the exponent is stored as a binary integer with a bias of 127(float) or 1023(double) added to it.
    When the number is normalised, the first digit, immediately before the binary point, will always be 1.
    This digit is made implicit and the remaining fraction part of the mantissa is stored in the next 23 bits (float) or 53 bits (double).
    The value of the number is
    (-1)**Sign * ( 1 + Mantissa ) 2**(Exponent - Bias)
    where
    Sign is the sign bit (0 or 1)
    Mantissa is the mantissa part with the point assumed at the start.
    Exponent is the exponent part treated as a normal binary integer.
    Bias is 127 for float, 1023 for double

    (see my Source for more details)

    The main problem is that
    the number of significant digit displayed can't be 'guessed' one for all ('a priori'):


    for example
    13176825*2**(-22) will be displayed with 5 digits 3.1416
    Whereas 11*2**(-20) will be displayed with 9 digits

    Hope this helps...

    Now for the Fun Pi/Perl part you could also check
    Arithmetic-Geometric Mean calculation of pi.
    or my modest <shameless plug> Simple Pi calculator </shameless plug>

    "Only Bad Coders Code Badly In Perl" (OBC2BIP)