in reply to Efficiently working with huge exponents

Just a quick calculation in a one-liner:
$ time perl -E 'my $c = log(2) * 1024000; my $d = $c / log(1e100); > $d = int $d; say $d; $c = $c - log(1e100) for 1..$d; > say $c; say "Value is: ", exp $c, " x 1e${d}00";' 3082 125.987232628425 Value is: 5.19469341155068e+54 x 1e308200 real 0m0.039s user 0m0.030s sys 0m0.000s
The last decimals are certainly wrong, but it seems that we can say that 5.194693e+308254 is a fair approximation, obtained in 39 milliseconds.

Update (at 16:36 UTC): A better version of more or less the same:

$ time perl -E 'my $c = log(2) * 1024000; my $d = int ( $c / log(1e10 +0)); > say $d; $c = $c - $d * log(1e100); > say $c; say "Value is: ", exp $c, " x 1e${d}00";' 3082 125.987232618965 Value is: 5.19469336241126e+54 x 1e308200 real 0m0.036s user 0m0.000s sys 0m0.046s
Update 2: fixed a wrong print out of the value.

Je suis Charlie.

Replies are listed 'Best First'.
Re^2: Efficiently working with huge exponents (yeah)
by tye (Sage) on Mar 20, 2015 at 17:53 UTC

    Yeah, that's the same type of thing that Math::BigApprox makes much easier:

    $ time perl -MMath::BigApprox=c -le'print c(2)**1024000' 5.194693e+308254 real 0m0.021s user 0m0.012s sys 0m0.004s

    And it makes a good guess at how many decimal places to show as well.

    - tye        

      Thank you, tye ++, I did not know about this module until today, it looks very interesting, I'll look at it and at its code.

      I was only trying to give a very basic proof of concept to try to help the OP. Surely, your module offers much more than my poor basic attempts. Congrats for it.

      Je suis Charlie.

        No, I'm glad you laid out the fairly simple math that lets one do these types of calculations quickly and fairly easily.

        I just saw the opportunity to connect the two suggestions and thus 1) give people a quick intro to what Math::BigApprox is doing under the covers and 2) let the OP know that they don't have to do this by hand if this level of accuracy is acceptable.

        Thanks for showing the math that I didn't have/take the time to. :)

        - tye        

      Thanks to you both. While I'll probably just use Math::BigApprox for my current need, re-learning a little math never hurt anyone (much).

        ... I'll probably just use Math::BigApprox for my current need ...

        When the original post first appeared, I didn't notice any mention of perfect precision *not* being desired. Was that part of the post always there ?
        Anyway, just to correct my original reply to cater for perfect precision *not* being required:
        perl -MMath::MPFR=:mpfr -le "Rmpfr_set_default_prec(28);print Math::MP +FR->new(2) ** 1024000;" 5.194693363e308254
        (In Math::MPFR, precision is specified in bits.)

        You *can* specify precision for your Math::BigFloat numbers, too - it's in the docs.

        Cheers,
        Rob