jocko2002 has asked for the wisdom of the Perl Monks concerning the following question:

Monks, I throw my unworthy knowledge at your feet for enlightenment. I wish only to understand. I have heard that the largest integer I can work with in Perl is 2,147,483,647 because this is the upper limit of 32 bits. And I have some code that uses the Set::IntSpan module and it does indeed start putting out invalid negative numbers over that amount. However, If I just do
#!/usr/local/bin/perl -w use strict; my $x = 3147483648; my $y = 3147483648; my $a = $x + $y; print $a;
I get the right answer. Why is this?

Eternally grateful for any wisdom offered.

Edit by tye

Replies are listed 'Best First'.
Re: Large Integers
by no_slogan (Deacon) on Apr 09, 2002 at 16:46 UTC
    In perl, numbers are double-precision floating point, unless you use integer;. Doubles have 52 bits of mantissa in many architectures, so they can represent integers that are quite a bit larger than 32-bit ints. They'll eventually run out of room and start rounding off, though.

    You can use larger integers by using Math::BigInt or the like, but I doubt you'll be able to get a module like Set::IntSpan to use bigints.

Re: Large Integers
by Fletch (Bishop) on Apr 09, 2002 at 16:55 UTC

    If you really need really big arbitrary precision numbers, use Math::BigInt or Math::BigFloat. Or recent perls can be compiled with support for 64bit ints, if your OS supports it.

(tye)Re: Large Integers
by tye (Sage) on Apr 10, 2002 at 15:45 UTC

    In Perl, certain things will only work with 32 bits. But math can handle integers up to about 56 bits.

    Checking the source for Set::IntSpan, I find the evil: use integer; so you can probably just comment out that line and suddenly be able to use 56-bit values. A note to the author to "fix" this would also be appreciated.

            - tye (but my friends call me "Tye")
Re: Large Integers
by rinceWind (Monsignor) on Apr 10, 2002 at 09:01 UTC
    Also note that the discussion on this thread is relevant