[Python-bugs-list] [ python-Bugs-499708 ] Int(long) causes Integer Overflow

noreply@sourceforge.net noreply@sourceforge.net
Fri, 22 Mar 2002 12:44:40 -0800


Bugs item #499708, was opened at 2002-01-05 03:46
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=499708&group_id=5470

Category: Python Interpreter Core
Group: Python 2.2
>Status: Closed
>Resolution: Later
Priority: 5
Submitted By: Raymond Hettinger (rhettinger)
Assigned to: Nobody/Anonymous (nobody)
Summary: Int(long) causes Integer Overflow

Initial Comment:
In the spirit of unifying longs and ints,
should int() be the sole way of truncating
floats or should the programmer be responsible
for categorizing and handling ints and longs
separately?

>>> int( 12341234123.4 )
Traceback (most recent call last):
  File "<interactive input>", line 1, in ?
OverflowError: float too large to convert
>>> def myint(f):
... 	try:
... 		return int(f)
... 	except OverflowError:
... 		return long(f)
... 	
>>> myint( 12341234123.4 )
12341234123L
>>> myint(123.4)
123

I have one misgiving about fixing this one.
assert type(int(x)) == int, 'should always succeed'
would fail for when x=12341234123.4
The only way around this is to make longs return
a IntType.  Who knows what this would break.


----------------------------------------------------------------------

>Comment By: Neil Schemenauer (nascheme)
Date: 2002-03-22 20:44

Message:
Logged In: YES 
user_id=35752

Closing and setting resolution to "Later" as suggested.

----------------------------------------------------------------------

Comment By: Martin v. Löwis (loewis)
Date: 2002-01-05 11:42

Message:
Logged In: YES 
user_id=21627

See PEP 237. Python 2.2 finished phase A, where only
operations are changed. Phase B.1 will address further
semantic differences, such as the builtin int function
(which will issue a warning if it returns a long where it
previously behaved differently). So I'd conclude that, for
Python 2.2, the behaviour you see is by design, and that
this report should be closed.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=499708&group_id=5470