Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: xrange() question

Reply
Thread Tools

Re: xrange() question

 
 
Bob Gailer
Guest
Posts: n/a
 
      08-05-2003
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(, xrange(0, sys.maxint)):

>... print n, x

[snip]
Consider:

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

....
2147483645

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

Does this give you any clues?

Bob Gailer
http://www.velocityreviews.com/forums/(E-Mail Removed)
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

 
Reply With Quote
 
 
 
 
George Trojan
Guest
Posts: n/a
 
      08-05-2003
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(, xrange(0, sys.maxint)):

>> ... print n, x

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

> ...
> 2147483645
>
> for x in xrange(sys.maxint-1,sys.maxint,2)
> ...
> Traceback (most recent call last):
> File "<interactive input>", line 1, in ?
> OverflowError: integer addition
>
> Does this give you any clues?
>
> Bob Gailer
> (E-Mail Removed)
> 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

 
Reply With Quote
 
 
 
 
Peter Abel
Guest
Posts: n/a
 
      08-06-2003
Bob Gailer <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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(, xrange(0, sys.maxint)):

> >... print n, x

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

> ...
> 2147483645
>
> for x in xrange(sys.maxint-1,sys.maxint,2)
> ...
> 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
> (E-Mail Removed)
> 303 442 2625
>
> --

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments