Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

maximum value of a scalar

by targetsmart (Curate)
on Oct 21, 2008 at 09:07 UTC ( #718414=perlquestion: print w/replies, xml ) Need Help??
targetsmart has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks, I made a scalar variable to hold an integer value and kept incrementing that scalar, now what is the maximum value that it can hold, exactly the unsigned int size or it will be different in perl.

Replies are listed 'Best First'.
Re: maximum value of a scalar
by JavaFan (Canon) on Oct 21, 2008 at 09:33 UTC
    It will be different in Perl. First of all, Perl doesn't know integers - at least not on the Perl language level. Perl has numerical values.

    Now, your question is ambiguous. I can put 1e100 in a scalar variable, and it will hold this. However, if I put 0 in a scalar, and repeatedly increment that by 1, I will never reach 1e100 because of loss of significancy.

    But even if your question is "what is the largest integer a scalar can hold without losing significancy?", then the answer is "it depends". It depends on whether your perl uses 64 bit integers, or 32 bit integers. In the former case, the answer is 2**63-1, or 9223372036854775807. If you have 32 bit integers, the answer is not 2**31-1, but much higher. perl then uses a double to store the integer, but a double has enough bits to not lose precision for a while. I don't know where exactly the cutoff is, but I recall it's around 2**53.

Re: maximum value of a scalar
by ikegami (Pope) on Oct 21, 2008 at 21:58 UTC

    For 32-bit version of Perl, the maximum integer (without using bigint or similar) that can be stored precisely is -253 on the negative side and 253 on the positive side.

    >perl -e"printf qq{%.f\n}, 2**53+$_ for +1,0,-1" 9007199254740992 9007199254740992 9007199254740991 >perl -e"printf qq{%.f\n}, -2**53+$_ for +1,0,-1" -9007199254740991 -9007199254740992 -9007199254740992
    build settingsMax positive integerMax negative integer
    32-bit ints and double floats253 = 9_007_199_254_740_992-253 = -9_007_199_254_740_992
    64-bit ints and double floats264-1 = 18_446_744_073_709_551_615-263 = 9_223_372_036_854_775_808
    64-bit ints and quadruple floats2113 = 10_384_593_717_069_655_257_060_992_658_440_192-2113 = -10_384_593_717_069_655_257_060_992_658_440_192

    Some operators (notably the bit operators) won't work with every number in that range.
    Some C extensions won't work with every number in that range.

      I'm trying to compile for quadruple floats, and it's not obvious to me what the parameter is during configure. Suggestions? Thanks, Scott
        I'm trying to compile for quadruple floats

        What is your compiler's C data type for these "quadruple floats" ? I had assumed that, if a C compiler could handle "quadruple floats", it would be handling them as "long doubles" - in which case building with -Duselongdouble might do the trick.

        Cheers,
        Rob
        Sorry, none. I have no knowledge on the topic.
Re: maximum value of a scalar
by BrowserUk (Pope) on Oct 21, 2008 at 16:12 UTC
Re: maximum value of a scalar
by ig (Vicar) on Oct 21, 2008 at 21:26 UTC

    You might find the answer to your question in http://perldoc.perl.org/perlnumber.html.

    As a reult of how Perl handles numbers, the answer to your question is somewhat complex and dependent on how your perl was compiled. Small integer numbers may be stored internally as integers or floating point numbers with no loss of information, but integers exceeding the limits of the internal integer representation can only be stored as floating point numbers. Floating point numbers have limited precision and for sufficiently large values the difference between one number and the next larger number that can be represented is greater than 1. At this limit, the numbers stop behaving as integers, at least for some operations. This is because, while larger integer values are possible, not all larger integer values are possible. For example, adding 1 to such a large value may not result in any change.

Re: maximum value of a scalar
by ccn (Vicar) on Oct 21, 2008 at 09:17 UTC
    ccn@laptop:~$ perl -MPOSIX -le 'print LONG_MAX' 2147483647 ccn@laptop:~$
      If that's not enough, you can always use Modules like Math::BigInt to use integers as large as you want (or at least up to the size of your computers memory).
        you can always use Modules like Math::BigInt

        Excellent observation - and such large numbers are, after all, stored in scalars - as per the OP's requirement.

        The one limitation of Math::BigInt is that it's pure perl and can therefore be a little slow when it comes to manipulating those numeric values. As an alternative one could look at modules like Math::GMP, Math::Pari and Math::MPFR. These modules, like Math::BigInt, all store their values in sclarars and they still suffer from the "available memory" limitation - and, unlike Math::BigInt, they all depend upon external C libraries. The one big difference in their favour is that they provide much faster arithmetic manipulation of the bignum values than Math::BigInt.

        Cheers,
        Rob
      That's wrong. LONG_MAX is a C constant, made available to you. It may say something about the maximum signed integer perl uses internally, but it's not the same maximum integer Perl can use. (Note the significant capitalization of the italic words).

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://718414]
Approved by holli
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (9)
As of 2018-07-16 20:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (349 votes). Check out past polls.

    Notices?