Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-04-24 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found