maximum value for long?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • S.Susnjar

    maximum value for long?

    Hello all,

    I have been programming in Python only for a month or so and have stumbled
    over a "problem" that I could not solve through rtfm.

    $ python
    Python 2.2.2 (#1, Nov 6 2003, 09:19:47)
    [GCC 3.3] on irix646
    Type "help", "copyright" , "credits" or "license" for more information.[color=blue][color=green][color=darkred]
    >>> ulong_max=18446 744073709551615
    >>> type(ulong_max)[/color][/color][/color]
    <type 'long'>[color=blue][color=green][color=darkred]
    >>> print ulong_max[/color][/color][/color]
    184467440737095 51615[color=blue][color=green][color=darkred]
    >>> very_long_long= 184467440737095 516151844674407 3709551615
    >>> type(very_long_ long)[/color][/color][/color]
    <type 'long'>[color=blue][color=green][color=darkred]
    >>> print very_long_long[/color][/color][/color]
    184467440737095 516151844674407 3709551615[color=blue][color=green][color=darkred]
    >>> result=very_lon g_long/15
    >>> type(result)[/color][/color][/color]
    <type 'long'>[color=blue][color=green][color=darkred]
    >>> print result[/color][/color][/color]
    122978293824730 344101229782938 247303441[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    I have set ulong_max above to the C compilers ULONG_MAX definition from limits.h
    I would have expected Python to complain when setting the value of the variable
    very_long_long to anything greater than ULONG_MAX.
    Since I am currently doing computations with integer values *much* bigger than
    ULONG_MAX, this behaviour suites me, but I am wondering if this is a "standard"
    Python behaviour since I do not want my scripts to become dependant on some
    obscure non-standard feature...

    Could any kind Python-Guru please shed some light on this?
    Also, if Python really can handle longs that are bigger than the machines native
    longs, the interpreter has to do some sort of arbitrary precision math
    somewhere. Could you please point me to the according interpreter's source file(s)?

    TIA + Regards,
    S.Susnjar
  • Skip Montanaro

    #2
    Re: maximum value for long?


    S> I have set ulong_max above to the C compilers ULONG_MAX definition
    S> from limits.h I would have expected Python to complain when setting
    S> the value of the variable very_long_long to anything greater than
    S> ULONG_MAX. Since I am currently doing computations with integer
    S> values *much* bigger than ULONG_MAX, this behaviour suites me, but I
    S> am wondering if this is a "standard" Python behaviour since I do not
    S> want my scripts to become dependant on some obscure non-standard
    S> feature...

    Python has a builtin unbounded long type. In 2.3, most (all?) operations on
    integers will return longs if the op would overflow the machine's int
    (typically 32- or 64-bits):
    [color=blue][color=green][color=darkred]
    >>> print 17**100[/color][/color][/color]
    110889937278078 364130611171587 509496643601716 764987952440276 984127888758050 136669771242469 425600509358924 845150306839760 8001

    S> Also, if Python really can handle longs that are bigger than the
    S> machines native longs, the interpreter has to do some sort of
    S> arbitrary precision math somewhere. Could you please point me to the
    S> according interpreter's source file(s)?

    Look at Objects/longobject.c in the source distribution.

    Skip

    Comment

    • Terry Reedy

      #3
      Re: maximum value for long?


      "S.Susnjar" <forth@gmx.de > wrote in message
      news:baab8c40.0 311180906.a4ab9 f7@posting.goog le.com...[color=blue]
      > Hello all,[/color]

      Hi.
      [color=blue]
      > I have been programming in Python only for a month or so and have[/color]
      stumbled[color=blue]
      > over a "problem" that I could not solve through rtfm.[/color]

      Ref Man 3.2 The standard type hierarchy
      "
      There are two types of integers:

      Plain integers
      These represent numbers in the range -2147483648 through 2147483647.
      (The range may be larger on machines with a larger natural word size,
      but not smaller.) When the result of an operation would fall outside
      this range, the exception OverflowError is raised. For the purpose of
      shift and mask operations, integers are assumed to have a binary, 2's
      complement notation using 32 or more bits, and hiding no bits from the
      user (i.e., all 4294967296 different bit patterns correspond to
      different values).

      Long integers
      These represent numbers in an unlimited range, subject to available
      (virtual) memory only. For the purpose of shift and mask operations, a
      binary representation is assumed, and negative numbers are represented
      in a variant of 2's complement which gives the illusion of an infinite
      string of sign bits extending to the left.
      "
      In short, Python ints are C longs, while Python longs are extended or
      arbitrary precision, and hence truly long. Your confusion over the
      two meanings of 'long' is not unique among people with a C background
      ;-).

      Terry J. Reedy





      Comment

      • Bengt Richter

        #4
        Re: maximum value for long?

        On Tue, 18 Nov 2003 21:45:45 -0500, "Terry Reedy" <tjreedy@udel.e du> wrote:
        [color=blue]
        >
        >"S.Susnjar" <forth@gmx.de > wrote in message
        >news:baab8c40. 0311180906.a4ab 9f7@posting.goo gle.com...[color=green]
        >> Hello all,[/color]
        >
        >Hi.
        >[color=green]
        >> I have been programming in Python only for a month or so and have[/color]
        >stumbled[color=green]
        >> over a "problem" that I could not solve through rtfm.[/color]
        >
        >Ref Man 3.2 The standard type hierarchy
        >"
        >There are two types of integers:
        >
        >Plain integers
        >These represent numbers in the range -2147483648 through 2147483647.
        >(The range may be larger on machines with a larger natural word size,
        >but not smaller.) When the result of an operation would fall outside
        >this range, the exception OverflowError is raised. For the purpose of[/color]

        Seems out of date re version 2.3.2:
        [color=blue][color=green][color=darkred]
        >>> x = 2**30
        >>> x[/color][/color][/color]
        1073741824[color=blue][color=green][color=darkred]
        >>> type(x)[/color][/color][/color]
        <type 'int'>[color=blue][color=green][color=darkred]
        >>> x+x[/color][/color][/color]
        2147483648L[color=blue][color=green][color=darkred]
        >>> type(x+x)[/color][/color][/color]
        <type 'long'>

        [color=blue]
        >shift and mask operations, integers are assumed to have a binary, 2's
        >complement notation using 32 or more bits, and hiding no bits from the
        >user (i.e., all 4294967296 different bit patterns correspond to
        >different values).
        >
        >Long integers
        >These represent numbers in an unlimited range, subject to available
        >(virtual) memory only. For the purpose of shift and mask operations, a
        >binary representation is assumed, and negative numbers are represented
        >in a variant of 2's complement which gives the illusion of an infinite
        >string of sign bits extending to the left.
        >"[/color]
        Though <rant>hex of negative numbers is slated for ugliness (IMO) in version 2.4
        in the form of '-' prefixing the hex of the absolute value -- making it useless
        for the normal bit pattern visualization that one would like when looking at bitwise
        operations. Ugh! Barf!</rant>.
        [color=blue]
        >In short, Python ints are C longs, while Python longs are extended or
        >arbitrary precision, and hence truly long. Your confusion over the
        >two meanings of 'long' is not unique among people with a C background
        >;-).
        >[/color]

        Regards,
        Bengt Richter

        Comment

        Working...