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

Howdy Monks!

If I'm not using bignum, bigint, or biganything; what kind of limits should I expect my scalars to have?

Do they have limits?

EXAMPLE!! (insert your own impatience)

#!/usr/bin/perl -w use strict; my $bestCounterEver = 0; my $nTimes = 10000000; my $lamePrintCounter = $nTimes; print "COUNTY!\n"; while (1) { $SIG{INT} = \&lazy; $bestCounterEver += 100000; $lamePrintCounter--; if ($lamePrintCounter < 0) { $lamePrintCounter = $nTimes; print "still going: $bestCounterEver\n"; } } sub lazy { print "Wait longer next time!!\n"; print "Final Count = $bestCounterEver \n"; exit(0); }

Replies are listed 'Best First'.
Re: Highest scalar = ???
by BrowserUk (Patriarch) on Oct 01, 2007 at 16:03 UTC

    On a system that uses IEEE 64-bit doubles:

    The maximum real a scalar can hold is 1.797693134862314e308

    The maximum integer a scalar can hold (accurately) is 9007199254740992


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      And (on a Perl using 32-bit ints),
      For the operations requiring an integer: 4294967295 (e.g. bitwise ops)
      For the operations requiring a signed integer: 2147483647 (e.g. for (a .. b))

      Just to be clear for the Anonymous Monk, a loop that keeps adding a constant to an integer does NOT roll around to turn negative. It merely turns the whole long integer to a real number, and adds the constant to that. A very small constant (like 1) will generally not even advance the real value, so such a loop will go forever.
      # will not work! $i = 1; while ($i > 0) { $i++ }
      Also see Equality checking - Comparing floats.

      --
      [ e d @ h a l l e y . c c ]

Re: Highest scalar = ???
by jwkrahn (Abbot) on Oct 01, 2007 at 17:28 UTC
    The POSIX module defines constants for numerical limits at FLOAT and LIMITS
    FLOAT Constants DBL_DIG DBL_EPSILON DBL_MANT_DIG DBL_MAX DBL_MAX_10_EXP + DBL_MAX_EXP DBL_MIN DBL_MIN_10_EXP DBL_MIN_EXP FLT_DIG FLT_EPSILON F +LT_MANT_DIG FLT_MAX FLT_MAX_10_EXP FLT_MAX_EXP FLT_MIN FLT_MIN_10_EXP FLT_M +IN_EXP FLT_RADIX FLT_ROUNDS LDBL_DIG LDBL_EPSILON LDBL_MANT_DIG LDBL_ +MAX LDBL_MAX_10_EXP LDBL_MAX_EXP LDBL_MIN LDBL_MIN_10_EXP LDBL_MIN_EXP LIMITS Constants ARG_MAX CHAR_BIT CHAR_MAX CHAR_MIN CHILD_MAX INT_MAX IN +T_MIN LINK_MAX LONG_MAX LONG_MIN MAX_CANON MAX_INPUT MB_LEN_MAX NAME_ +MAX NGROUPS_MAX OPEN_MAX PATH_MAX PIPE_BUF SCHAR_MAX SCHAR_MIN SHRT_MAX + SHRT_MIN SSIZE_MAX STREAM_MAX TZNAME_MAX UCHAR_MAX UINT_MAX ULONG_MA +X USHRT_MAX
Re: Highest scalar = ???
by samtregar (Abbot) on Oct 01, 2007 at 17:21 UTC
    The highest integer is easy to get:

    $ perl -e 'print ~0' 4294967295

    Perl can count higher by using floating point, but that's the last exact integer you'll be able to represent without Math::BigInt.

    -sam

      That's not true. Floating points can represent a large range of integers exactly, and that range for a double is bigger than the range of an unsigned 32 bit int.

      Type Min Max -------------------------------- -------- ----------- signed n-bit int -2^(n-1) (2^(n-1))-1 unsigned n-bit int 0 (2^n)-1 IEEE float with n-bit mantissa -2^(n+1) 2^(n+1)

      Double has n=52, so 4294967295 isn't the maximum that can be held without Math::BigInt.

      Type Min Max -------------------------------- ----------------- ---------------- signed 32-bit integer -2147483648 2147483647 unsigned 32-bit integer 0 4294967295 IEEE float with 52-bit mantissa -9007199254740992 9007199254740992

      Demo:

      $x = 2**(52+1); # For doubles, format "%.17e" will tell you if # the number is stored with no loss of precision # because log10(2**(n+1)) < 17. printf("%.17e\n\n", $x); $x -= 5; for (1..10) { printf("%f\n", $x); $x++; }
      9.00719925474099200e+015 <-- That's exact. 9007199254740987.000000 9007199254740988.000000 9007199254740989.000000 9007199254740990.000000 9007199254740991.000000 9007199254740992.000000 9007199254740992.000000 <-- Least significant 9007199254740992.000000 bit dropped. 9007199254740992.000000 9007199254740992.000000
        I suppose that depends on your definition of "integer". I was never much of a math student, so I use the comp. sci definition. So does Perl, sometimes:

        $x = 2**(52+1); for (1 .. $x) { # not gonna get here! }

        Produces:

           Range iterator outside integer range at foo.pl line 2.

        Guess that's not an integer after all!

        -sam

Re: Highest scalar = ???
by mr_mischief (Monsignor) on Oct 01, 2007 at 16:23 UTC
    Perl traditionally uses 32-bit integers, but you can have 64-bit integers if your interpreter is configured that way at compilation time. This can break compatibility with pre-compiled modules.

    Natively 64-bit systems like Alpha don't need a configuration flag set.

    Perl automatically converts to floating point when integers can't hold a value, too.

    perldelta for 5.6 has all the nooks and crannies of 64-bitness.

    perlnumber has MTYEWTK about numbers in Perl.

Re: Highest scalar = ???
by swampyankee (Parson) on Oct 01, 2007 at 22:02 UTC

    Read this about floating point. The greatest possible integer will depend on the platform; typically it will be 2^64-1 or 2^32-1 for unsigned ints or 2^31-1 or 2^63-1 for signed integers. There may be odd machines that are neither 32 nor 64 bit or don't use twos-complement arithmetic(the Univac 1108 used 48 bit integers and ones-complement arithmetic); these will have different values.


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.

      If you want bigger integers, use bigint;
      Read perldoc bigint