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

is there a boundry that perl will impose on an interger or will it let them grow indefinatly?

Replies are listed 'Best First'.
Re: about int's
by Zaxo (Archbishop) on Nov 08, 2003 at 22:47 UTC

    Depending on how perl is built, int's are either 32 or 64 bits. The core Math::BigInt module allows the use of effectively unlimited size integers.

    After Compline,
    Zaxo

Re: about int's
by ysth (Canon) on Nov 09, 2003 at 03:38 UTC
    Use Math::BigInt to let them grow unboundedly. (This module underwent a major rewrite at one point. Use the version shipped with perl 5.8 or later; for earlier perl versions, get a copy from CPAN).

    Normally, perl uses a 32 or 64 bit signed integer (depending on platform and Configure options), promoting it to a floating point value (usually with 53 or 64 bits of mantissa)/ as needed. To stretch just a little extra range for integers, perl will sometimes use an unsigned integer instead of a signed one.

      Oh, and when using Math::BigInt, do make sure you're using one of those lower level supporting XS libraries, or it'll be damn slow — all lower level calculations being done in Pure Perl. I'm not sure if the Math::BigInt that comes with 5.8 is that smart by itself, but older versions definitely weren't.

      See the Math::BigInt docs for more info. No, it doesn't look like it's gotten any smarter.

        Even with one of the XS libraries, it'll be a lot slower.
Re: about int's
by The Mad Hatter (Priest) on Nov 08, 2003 at 22:49 UTC
    I believe the only restriction on an integer in Perl is the same restriction C ints are restricted by: the architecture (is there a more proper term?) of the chip. This is most likely 32 bits (unsigned?), but could be 64.

    Update Oops. See Abigail's post below.

      No, it's certainly *not* the same as C ints, and it's not necessarely directly related to size of registers on the hardware. For internal integers, you can get 32 bit or 64 bit integers (or perhaps even 128 bit integers) if your C compiler supports it.

      But under normal conditions, Perl (the language) doesn't have integers. It has numerical values. They may internally represented by integers, but if the need arises, they are seemlessly upgraded to floats. And you need to jump to some hoops to find out the internal representation. The precision of floats may vary from platform to platform, but it's typically 64 bits. This means that if your perl uses 32 bit integers internally, Perl (the language) still is able to deal with integer numbers of around 53 bits without losing precision.

      Abigail