This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: int(float) may return a long for no reason
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 2.7
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: arigo, loewis, mark.dickinson, python-dev
Priority: normal Keywords: patch

Created on 2011-02-07 17:45 by arigo, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
issue11144.patch mark.dickinson, 2011-02-09 20:40 review
Messages (7)
msg128143 - (view) Author: Armin Rigo (arigo) * (Python committer) Date: 2011-02-07 17:45
On 32 bits, there is no reason to get a 'long' here:

>>> int(float(sys.maxint))
2147483647L
>>> int(int(float(sys.maxint)))
2147483647
>>> int(float(-sys.maxint-1))
-2147483648L
>>> int(int(float(-sys.maxint-1)))
-2147483648

On 64 bits, it's another story because floats cannot store 64 bits of precision.  However, -sys.maxint-1 can still be represented exactly in a float, and the same issue occurs:

>>> int(float(-sys.maxint-1))
-9223372036854775808L
>>> int(int(float(-sys.maxint-1)))
-9223372036854775808
msg128156 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2011-02-07 21:15
I'm not sure whether this is a bug, though. So I'd be skeptical that it should be changed in 2.x, even if the cause was identified.
msg128167 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-02-08 08:23
I'd call this a bug, albeit a minor one.  The documentation for 'int' says: 

"If the argument is outside the integer range a long object will be returned instead."

... which certainly suggests (without actually formally implying) that an int object should be returned if the argument is within the integer range.
msg128189 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2011-02-08 20:16
> I'd call this a bug, albeit a minor one.  The documentation for 'int' says: 
> 
> "If the argument is outside the integer range a long object will be returned instead."

Ah ok. I agree it's a bug, then.
msg128237 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-02-09 20:40
This turns out to be a one-line fix (modulo comments and tests); see attached patch.  The new code depends on the assumption that C longs are represented using two's complement, but note that this assumption is (a) already present elsewhere in the Python core (e.g., in the definition of bitwise operations for ints), and (b) universally true in practice (as far as I'm aware).  For theoretical completeness, it would be easy to write a different test for ones' complement and sign-magnitude machines, but in practice that seems pointless, and given the near-impossibility of testing that code, I left it out.
msg128240 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2011-02-09 20:52
Very smart!
msg132234 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-03-26 12:30
New changeset e1ebec2446cd by Mark Dickinson in branch '2.7':
Issue #11144: Fix corner cases where float-to-int conversion unnecessarily returned a long.
http://hg.python.org/cpython/rev/e1ebec2446cd
History
Date User Action Args
2022-04-11 14:57:12adminsetgithub: 55353
2011-03-26 12:31:42mark.dickinsonsetstatus: open -> closed
type: behavior
stage: resolved
resolution: fixed
assignee: mark.dickinson
2011-03-26 12:30:18python-devsetnosy: + python-dev
messages: + msg132234
2011-02-09 20:52:24loewissetmessages: + msg128240
2011-02-09 20:40:08mark.dickinsonsetfiles: + issue11144.patch

messages: + msg128237
keywords: + patch
2011-02-08 20:16:27loewissetmessages: + msg128189
2011-02-08 08:23:42mark.dickinsonsetmessages: + msg128167
2011-02-08 07:51:22mark.dickinsonsetnosy: + mark.dickinson
2011-02-07 21:15:52loewissetnosy: + loewis
messages: + msg128156
2011-02-07 17:45:54arigocreate