Hello Monks!

I need to work with some rather large exponents (much too big for ordinary Perl scalar), on the order of 2**1024000.

Here's my test program:

#!/usr/bin/env perl use 5.014; use Math::BigFloat; my $POW = 1024000; my $n = Math::BigFloat->new(2); say "BigFloat result: " . $n->pow($POW);

On my system, this takes over 3 minutes to run, and works with perfect precision (i.e., I get a 308255-digit integer). I don't need perfect precision, so I set $n->accuracy(10);, which is plenty for my purposes. However, I still get a huge integer as a result (only this time, it's mostly zeroes), and it still takes over 3 minutes to run.

What I want is a limited-precision floating point number such as 5.194693e308255, that I can compare to other such numbers for sorting, display, and the like. Any more than, say, 10 significant figures, is a waste.

Is there a way (with or without BigFloat) to (quickly) calculate a limited-precision float like 5.195e308255? I feel like I'm missing something obvious.


In reply to Efficiently working with huge exponents by wanna_code_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.