Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: Big Numbers

by grinder (Bishop)
on Jul 22, 2002 at 14:06 UTC ( [id://184051]=note: print w/replies, xml ) Need Help??


in reply to Big Numbers

2 raised to the 64th power is only 18 446 744 073 709 551 616, or around 18 and a half quintillion. Or about 16 exabytes. Which is fantistically huge, or woefully inadequate, depending on what your needs are.

Math::BigInt lets you define numbers as  my $num = Math::BigInt->new( '18 446 744 073 709 551 617' ) which is as about as clean as it gets.

Internally, the package uses base 10 arithmetic, not base 2 (or 16), which is why I suspect you can't enter numbers in hex format. I suppose no-one has got around to patching the package to do it. Converting between base 10 and base 16 is algorithmically fairly trivial but computationally quite expensive, as you have to perform repeated divisions. (Update: to be precise, converting from hex to dec is easy. I was thinking about converting from decimal to hex... unless there's something I've missed, you can't do it with lookup tables).

Your easiest bet would be to write a program that takes an arbirtarily long hex string and converts it to base 10, and use that constant in your program. For extra credit, you could patch the Math::BigInt package to allow the 0x prefix to signify a hex representation, and have the package decode it.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'

Replies are listed 'Best First'.
Re: Big Numbers
by Abigail-II (Bishop) on Jul 22, 2002 at 14:24 UTC
    Repeated division? How come? Here's a simple routine that translates hex numbers into decimal ones. Replace the my $answer = 0; with my $answer = Math::BigInt -> new (0); to extend it bigger numbers.
    my $c = 0; my %X = map {$_ => $c ++} 0 .. 9, 'A' .. 'F'; sub hex2dec { local $_ = shift; my $answer = 0; ($answer <<= 4) += $X {substr $_ => 0, 1, ""} while length; $answer; }
    Abigail
Re: Re: Big Numbers
by RollyGuy (Chaplain) on Jul 22, 2002 at 15:32 UTC
    Thanks for the Math::BigInt link. I found that I was using an older version of the Math::BigInt package. I was using the autocreated constant feature where any constant in the current scope is converted into a BigInt. I had success with integers, but not hex values. The documentation now claims hex constants will be converted as well. All I have to do (according to the documentation) is:
    use Math::BigInt qw/:constant/;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://184051]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-04-25 15:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found