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;
}
-
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.