[ python-Bugs-1770416 ] Decimal.__int__ overflows for large values

SourceForge.net noreply at sourceforge.net
Thu Aug 9 10:09:29 CEST 2007


Bugs item #1770416, was opened at 2007-08-08 17:43
Message generated for change (Comment added) made by ajaksu2
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1770416&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Private: No
Submitted By: Jason G (aryx)
Assigned to: Nobody/Anonymous (nobody)
Summary: Decimal.__int__ overflows for large values

Initial Comment:
This also affects Decimal.__hash__, since it [indirectly] calls Decimal.__int__.

>>> from decimal import Decimal as D
>>> e = D("1e1234567890987654321")
>>> int(e)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/decimal.py", line 1501, in __int__
    s = ''.join(map(str, self._int)) + '0'*self._exp
OverflowError: cannot fit 'long' into an index-sized integer
>>> e = D("1e1234567890")
>>> int(e)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python2.5/decimal.py", line 1501, in __int__
    s = ''.join(map(str, self._int)) + '0'*self._exp
MemoryError

Also, for values that do work this is incredibly slow if they are still fairly large.

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

Comment By: ajaksu (ajaksu2)
Date: 2007-08-09 05:09

Message:
Logged In: YES 
user_id=1200609
Originator: NO

Hi Jason,
The OverflowError is related to "index-sized ints" as in "ints that are
valid indexes for sequences", try:
>>> e = "0" * 1234567890

So it seems that this error is avoiding the creation of a string of length
1234567890, which is a good thing (sorta) :)

Once I tried to implement a dec2long function that was based on numbers
instead of strings, see if it helps (it's VERY slow and naive, but IIRC it
was a bit faster than the original version and correct):
http://groups.google.com/group/comp.lang.python/msg/aba7264ab38eb25e

Now, do you really need all that precision for such huge numbers? I know I
didn't ;)
Daniel

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

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


More information about the Python-bugs-list mailing list