Math::BigInt:

sub blog10 { my $bn = shift; require Math::BigInt; return (ref($bn) ? $bn->copy : Math::BigInt->new($bn))->blog(10); } my $bn = 1e12; my $blog10 = blog10($bn); print "blog10($bn): $blog10\n"; print "mod($blog10,3): ", $blog10 % 3, "\n"; print "int($blog10): ", int($blog10), "\n";

The output:

blog10(1000000000000): 12 mod(12,3): 0 int(12): 12

...which should be what you expect it to be.

This does silently upgrade your number to a Math::BigInt object. Calling ref($blog10) would return Math::BigInt, for example. If that's objectionable, modify your blog10 function as follows:

return (ref($bn) ? $bn->copy : Math::BigInt->new($bn))->blog(10)->numi +fy;

You could make this even more "black box" by auto-decorating and auto-undecorating based on logic:

sub blog10 { my $n = shift; my $bn = ref($n) ? $n->copy : do { require Math::BigInt; Math::BigInt->new($n) }; my $ret = $bn->blog(10); return ref($n) ? $ret : $ret->numify; }

Dave


In reply to Re: Clean log_10 ? by davido
in thread Clean log_10 ? by LanX

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.