Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

64 bit numbers in a 32 bit world.

by TedYoung (Deacon)
on Apr 24, 2007 at 20:47 UTC ( [id://611847]=perlquestion: print w/replies, xml ) Need Help??

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
    A couple of weeks ago I released Math::Int64 that does exactly what you ask for. Though it is yet not stable, may contain bugs and requires 64bits support from the C compiler.
Re: 64 bit numbers in a 32 bit world.
by ikegami (Patriarch) on Apr 24, 2007 at 21:07 UTC

    But, printing $i still seems to hold the large number despite $i not being a Math::BigInt.

    I'm betting $i is just a string. useDevel::Peek; Dump($i); would confirm that.

    Two pedantic notes,

    • Your do { ... }; blocks could have been written as { ... }. It would make a difference if you used next, redo or last in the block, however.
    • I don't see anything that says that bignum is lexically scoped. It's misleading to place it in a block if it's not.

      I couldn't find anything about bignum being lexically scoped either, but try:

      { use bignum; print ref 1; # Math::BigInt } { print ref 1; # <blank> }

      Perl just dwim.

      Ted Young

      ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
Re: 64 bit numbers in a 32 bit world.
by rodion (Chaplain) on Apr 24, 2007 at 21:11 UTC
    I believe DBI is returning a string to you, which Perl has no trouble representing. If it's already a string, print can easily display it. I think you would have trouble if you tried to display it with "printf('%20d', $i);".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://611847]
Approved by Corion
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: (4)
As of 2024-04-25 13:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found