in reply to Re^7: use64bitint in building Perl
in thread use64bitint in building Perl

if numbers do go over 2 GB, Perl automatically upgrades you to floats

If perl has been built with use64bitint and without uselongdouble, the "upgrade" can happen before the 2GB limit is reached ... which is definitely not what you want (as it means you've lost precision):
$ perl -MDevel::Peek -le 'Dump(17 + (2 ** 56))' SV = NV(0x10450940) at 0x10410fd0 REFCNT = 1 FLAGS = (PADBUSY,PADTMP,NOK,READONLY,pNOK) NV = 7.2057594037928e+16
In this instance, the workaround is to 'use integer':
$ perl -Minteger -MDevel::Peek -le 'Dump(17 + (2 ** 56))' SV = IV(0x10429358) at 0x10410fdc REFCNT = 1 FLAGS = (PADBUSY,PADTMP,IOK,READONLY,pIOK) IV = 72057594037927953
Just a little trap for the unwary.

Cheers,
Rob

Update: Ooops - where *I* wrote 2GB above, I should have written "64-bit". And my point is unrelated to tilly's, anyway ....

Replies are listed 'Best First'.
Re^9: use64bitint in building Perl
by ikegami (Patriarch) on Dec 30, 2008 at 02:29 UTC

    Your code doesn't demonstrate what you claim for two reasons.

    • 2**56 is 33,554,432 times larger than 2GB (2**31).
    • You didn't show any loss of precision (even though there was some).
      Your code doesn't demonstrate what you claim for two reasons

      You're quite right :-)
      Post updated.

      Cheers,
      Rob
Re^9: use64bitint in building Perl
by tilly (Archbishop) on Dec 30, 2008 at 02:22 UTC
    Not so fast on losing precision. IEEE binary floating-point for double precision (which is what I believe Perl uses) represents small integers exactly. Where small is anything under 53 bits. So the upgrade wastes memory, but doesn't necessarily lose any precision.
Re^9: use64bitint in building Perl
by ikegami (Patriarch) on Dec 30, 2008 at 02:31 UTC

    Your code doesn't demonstrate what you claim for two reasons.

    • 2**56 is 33,554,432 times larger than 2GB (2**31).
    • You didn't show any loss of precision (even though there was some).