Re: converting big hex to dec
by davidrw (Prior) on Sep 27, 2005 at 13:09 UTC
|
Your real speed loss is going to be from the spawning of a new process via the backticks, not the actual number conversion. As blazar also noted, you can use Math::BigInt within the scope of a specific block. So something like this will work (and this could easily be put in a loop):
perl -le '$x = do {use Math::BigInt qw/:constant/; 2**96; }; print $x
+; print 2**96'
| [reply] [d/l] |
Re: converting big hex to dec
by blazar (Canon) on Sep 27, 2005 at 11:55 UTC
|
Whatever, I wouldn't call another perl instance just to do a calculation. You know that pragmas like bigint import semantincs into block scopes, don't you?
$ perl -le '{use bigint; print 2**96} print 2**96'
79228162514264337593543950336
7.92281625142643e+28
I hope that someone will give you a more appropriate answer though... | [reply] [d/l] |
Re: converting big hex to dec
by BrowserUk (Patriarch) on Sep 27, 2005 at 12:04 UTC
|
Update: Apparently I should point out that this code (obviously) has the same limitation as any code that uses Perl's NV (double) format numbers, it is limited to 53-bits of precision.
sub hex16dec{
my( $hi, $lo ) = split '(.{8}$)', $_[0];
return hex( $hi ) * 2**32 + hex( $lo );
}
$value = '123456789ABCDEF';
printf "%.f\n", hex16dec $value;
81985529216486896
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
| [reply] [d/l] |
|
|
Unfortunately that will exceed the 15 digit precision of a 64bit double. In fact, your answer doesn't match the result from the 64bit machine shown above, or this:
> perl -Mbigint -le 'print 0x123456789ABCDEF'
81985529216486895
--
John.
| [reply] [d/l] |
|
|
That's why I chose to show that particular value, to point out the limitation and make the OP think about what he is doing.
If the OP's task is simply to convert ascii-hex to ascii-decimal, why is he (clumsily) restricting the scope of BigInt?
The only reason for limiting it's scope that came to mind, is if he is doing math within the script that he doesn't want slowed by BigInt being involved, but if that is the case, then he should also realise that the moment he moves the 64-bit integer outside of the scope of BigInt, he faces the possibility of loss of precision when the values are greater than 53-bits.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
| [reply] |
|
|
|
|
| [reply] [d/l] |
|
|
| [reply] [d/l] [select] |
Re: converting big hex to dec
by Anonymous Monk on Sep 27, 2005 at 11:54 UTC
|
Much faster? Compiler yourself a 64bit perl.
$ perl -le 'print 0x0123456789ABCDEF'
81985529216486895
| [reply] [d/l] |
|
|
Hehe, indeed the first time I had access to one of the 64-bit machines our lab just bought you bet the first thing I did was
$ perl -le print~0
(not there now so I won't paste the output -- nor fake it!) | [reply] [d/l] |
|
|
You don't need 64bit hardware to have 64bit integers in Perl.
| [reply] |
|
|
|
|