TedYoung has asked for the wisdom of the Perl Monks concerning the following question:
Hi, I am using perl 5.8.4 (compiled 32 bit). I want to create 64 bit numbers and insert them into a mysql db using DBI. Until now, I have always used mysql's bigint column (64 bit int) with Perl/DBI with no problem. But, I never actually used numbers greater than 32 bits. Now I need to.
Is there a way to create a number between 1 << 32 and 1 << 64 without using bigint? I don't have any reason not to use bigint, other than I don't need infinite precision, and would expect (emulated) 64 bit arithmetic to be faster. That may not actually be true, though.
So, I asked DBI and DBD::mysql what they thought about this. Here is my code. The comments show the output of each print.
use DBI; use Data::Dumper; my $db = DBI->connect("DBI:mysql:dev", '*****', '******', { RaiseError + => 1 }); $db->do("drop table test"); $db->do("create table test (foo bigint)"); print '---------------------------------------------'; do { use bigint; # Need this to make larger numbers my $i = 1 << 62; print $i; # 4611686018427387904 print ref $i; # Math::BigInt $db->do('insert into test values(?)', {}, $i); }; print '---------------------------------------------'; do { my $a = $db->selectrow_arrayref('select * from test'); my $i = $a->[0]; print $i; # 4611686018427387904 print ref $i; # <blank> print 0 + $i; # 4.61168601842739e+18 };
Some comments: The last statement (0 + $i) rounds the number to a floating point if I don't use bigint (as expected). But, printing $i still seems to hold the large number despite $i not being a Math::BigInt. Even using biging in the second do does not cast the $i to a Math::BigInt (but at least arithmetic works).
How does DBI make that? <whine>I want to too!!!</whine>
FWIW, perlop says the shift opts are undefined for counts larger than 31 outside of bigint
Update: Thanks monks! I am not sure why string didn't occur to me! That makes a lot of sense. I will check out Math::Int64 too.
Thanks,
Ted Young
($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: 64 bit numbers in a 32 bit world.
by salva (Canon) on Apr 24, 2007 at 21:11 UTC | |
|
Re: 64 bit numbers in a 32 bit world.
by ikegami (Patriarch) on Apr 24, 2007 at 21:07 UTC | |
by TedYoung (Deacon) on Apr 25, 2007 at 02:31 UTC | |
|
Re: 64 bit numbers in a 32 bit world.
by rodion (Chaplain) on Apr 24, 2007 at 21:11 UTC |