penguin_asylum has asked for the wisdom of the Perl Monks concerning the following question:

Hi...

I am making a perl program where the user inputs a number, and the program returns that amount of digits of the numeric constant "pi." I have found an algorithm, and gotten the program working besides one thing:

Of course perl only stores 15 digits in the variable.

Is there a way (including a way using modules etc.) to store more than that in a variable? If not, it would be helpful if anyone had any ideas of how I could get around this problem.

Thanks...
  • Comment on Storing more decimal places in a variable

Replies are listed 'Best First'.
Re: Storing more decimal places in a variable
by Zaxo (Archbishop) on Dec 15, 2004 at 23:35 UTC

    Math::BigFloat is a core module distributed with Perl. It's just what you want.

    After Compline,
    Zaxo

      Thank you very much :P

      now just to nag my webserver to install it...

      and a quick question on syntax for Math::BigFloat :

      first of all, if I say "Math::BigFloat->precision(5);" it will only apply to results from "$x->bmul($y);" etc., correct? thanks again for your prompt help :P
        The docs say "All operators (inlcuding basic math operations) are overloaded", which means it should work work *, +, etc. (provided one of the operands is of class Math::Float). Try and find out.
        now just to nag my webserver to install it...

        It comes with the standard distribution, so you probably already have it.

        use Math::BigFloat; Math::BigFloat->accuracy(200); $x = Math::BigFloat->new(2); print $x->bsqrt();


        -- All code is 100% tested and functional unless otherwise noted.
      If using BigFloat or BigInt, be mindful that they are SLOW. They can handle 'big' numbers because they're not bound by the word-length limitations of your CPU's add/sub/mul/div instructions, as they implement their own versions in perl storing the data in encoded strings. Trouble is, there's an awful lot of overhead in doing this.

      I have some code which uses BigInt (it's in Net::CIDR, for dealing with IPv6 addresses) and even taking account of the fact that the BigInt version deals with more data than the int version, it still runs at of the order of 1/100th to 1/1000th or worse of the speed.

        It doesn't sound as though the OP's application needs blazing speed for MP arithmetic. In any case, Math::BigFloat's speed can be improved by using an external library like Pari or GMP, eg. use Math::BigFloat lib => 'GMP';

        After Compline,
        Zaxo

Re: Storing more decimal places in a variable
by sleepingsquirrel (Chaplain) on Dec 16, 2004 at 00:32 UTC
    You could also think of using a different algorithm, one which doesn't depend on perl's floating point numbers. Here I'm thinking of something like the Unbounded Spigot Algorithms for the Digits of π...
    Rabinowitz and Wagon call their algorithm a spigot algorithm, because it yields digits incrementally, and does not use digits after they have been computed. The digits drip out one by one, as if from a leaky tap. In contrast, most algorithms for computing the digits of π deliver no output until the whole computation is completed.
    Update: fixed broken link.


    -- All code is 100% tested and functional unless otherwise noted.
      That link appears to be broken... but thanks for the info anyway :P
Re: Storing more decimal places in a variable
by mattr (Curate) on Dec 17, 2004 at 02:20 UTC
    Funny, when I first read the title I had a flash vision of appending more digits as letters in a string.

    I wonder if there is any code for arithmetic overrides using base 26 (or 36). On second thought, if you prefix with a single alphabet letter you should be able to use base 10 within a string. I guess BigInt does something smarter.