Re: xrange() question

Discussion in 'Python' started by Bob Gailer, Aug 5, 2003.

  1. Bob Gailer

    Bob Gailer Guest

    At 02:27 PM 8/5/2003 +0000, George Trojan wrote:

    >Why do I get an overflow error here:
    >
    > > /usr/bin/python

    >Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    >[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    >Type "help", "copyright", "credits" or "license" for more information.
    > >>> import sys
    > >>> for n, x in zip(range(4), xrange(0, sys.maxint, 2)):

    >... print n, x
    >...
    >Traceback (most recent call last):
    > File "<stdin>", line 1, in ?
    >OverflowError: integer addition
    >
    >but not here:
    >
    > >>> for n, x in zip(range(8), xrange(0, sys.maxint)):

    >... print n, x

    [snip]
    Consider:

    >>> for x in xrange(sys.maxint-2,sys.maxint,2):x

    ....
    2147483645

    for x in xrange(sys.maxint-1,sys.maxint,2):x
    ....
    Traceback (most recent call last):
    File "<interactive input>", line 1, in ?
    OverflowError: integer addition

    Does this give you any clues?

    Bob Gailer

    303 442 2625


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003
    Bob Gailer, Aug 5, 2003
    #1
    1. Advertising

  2. Bob Gailer wrote:

    > At 02:27 PM 8/5/2003 +0000, George Trojan wrote:
    >
    >> Why do I get an overflow error here:
    >>
    >> > /usr/bin/python

    >> Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    >> [GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    >> Type "help", "copyright", "credits" or "license" for more information.
    >> >>> import sys
    >> >>> for n, x in zip(range(4), xrange(0, sys.maxint, 2)):

    >> ... print n, x
    >> ...
    >> Traceback (most recent call last):
    >> File "<stdin>", line 1, in ?
    >> OverflowError: integer addition
    >>
    >> but not here:
    >>
    >> >>> for n, x in zip(range(8), xrange(0, sys.maxint)):

    >> ... print n, x

    >
    > [snip]
    > Consider:
    >
    > >>> for x in xrange(sys.maxint-2,sys.maxint,2):x

    > ...
    > 2147483645
    >
    > for x in xrange(sys.maxint-1,sys.maxint,2):x
    > ...
    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in ?
    > OverflowError: integer addition
    >
    > Does this give you any clues?
    >
    > Bob Gailer
    >
    > 303 442 2625
    >
    >------------------------------------------------------------------------
    >
    >
    >---
    >Outgoing mail is certified Virus Free.
    >Checked by AVG anti-virus system (http://www.grisoft.com).
    >Version: 6.0.506 / Virus Database: 303 - Release Date: 8/1/2003
    >
    >

    It does. The reason I asked the question is that in the original example
    zip() needs only 4 elements from xrange(), so I was puzzled why the
    overflow was reached. But the following:

    >>> xrange(0, 3, 2)

    xrange(0, 4, 2)

    explains it (I think). Thanks to all for the answers.

    George
    George Trojan, Aug 5, 2003
    #2
    1. Advertising

  3. Bob Gailer

    Peter Abel Guest

    Bob Gailer <> wrote in message news:<>...
    > At 02:27 PM 8/5/2003 +0000, George Trojan wrote:
    >
    > >Why do I get an overflow error here:
    > >
    > > > /usr/bin/python

    > >Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
    > >[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
    > >Type "help", "copyright", "credits" or "license" for more information.
    > > >>> import sys
    > > >>> for n, x in zip(range(4), xrange(0, sys.maxint, 2)):

    > >... print n, x
    > >...
    > >Traceback (most recent call last):
    > > File "<stdin>", line 1, in ?
    > >OverflowError: integer addition
    > >
    > >but not here:
    > >
    > > >>> for n, x in zip(range(8), xrange(0, sys.maxint)):

    > >... print n, x

    > [snip]
    > Consider:
    >
    > >>> for x in xrange(sys.maxint-2,sys.maxint,2):x

    > ...
    > 2147483645
    >
    > for x in xrange(sys.maxint-1,sys.maxint,2):x
    > ...
    > Traceback (most recent call last):
    > File "<interactive input>", line 1, in ?
    > OverflowError: integer addition
    >
    > Does this give you any clues?
    >

    YES, it does.
    At my opinion it's completely in accordance with my above said.
    You start with sys.maxint - 1 = 2147483646 and then get this
    as first element. Next - I guess - Python will check if 2147483646 + 2
    is bigger or equal than the ending criteria, what is sys.maxint. But this
    check will fail because 2147483646 + 2 causes an overflow.
    So for me the global rule is:
    If you have a therm like **range(start,end,step)**,
    it is higly recommmended, that "start" + "N" * "step" is equal to "end" for a
    certain "N", where "N" is an integer. This is necessary when "end" is
    sys.maxint.

    Regards
    Peter


    > Bob Gailer
    >
    > 303 442 2625
    >
    > --
    Peter Abel, Aug 6, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Christopher T King

    xrange() syntactic sugar

    Christopher T King, Jan 9, 2004, in forum: Python
    Replies:
    0
    Views:
    314
    Christopher T King
    Jan 9, 2004
  2. Gerrit Holl

    xrange not hashable - why not?

    Gerrit Holl, Jan 25, 2004, in forum: Python
    Replies:
    6
    Views:
    328
    Peter Otten
    Jan 25, 2004
  3. Hans Nowak

    Re: xrange not hashable - why not?

    Hans Nowak, Jan 26, 2004, in forum: Python
    Replies:
    1
    Views:
    334
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
    Jan 26, 2004
  4. Christian Neumann

    Problem with Python xrange

    Christian Neumann, Jun 6, 2004, in forum: Python
    Replies:
    7
    Views:
    2,758
    Terry Reedy
    Jun 8, 2004
  5. _wolf
    Replies:
    11
    Views:
    460
    Arnaud Delobelle
    Apr 18, 2009
Loading...

Share This Page