Mark Dickinson added the comment:

Sorry:  that was nonsense.  trunc is fine---it's round, floor and ceil that 
fail on large arguments with this patch:

>>> import decimal, math
>>> math.floor(decimal.Decimal("1e30"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dickinsm/python_source/py3k/Lib/decimal.py", line 1475, in 
__floor__
    return trunc(self.quantize(Decimal(1), rounding=ROUND_FLOOR))
  File "/Users/dickinsm/python_source/py3k/Lib/decimal.py", line 2265, in 
quantize
    'quantize result has too many digits for current context')
  File "/Users/dickinsm/python_source/py3k/Lib/decimal.py", line 3546, in 
_raise_error
    raise error(explanation)
decimal.InvalidOperation: quantize result has too many digits for current 
context
>>> round(decimal.Decimal("1e30"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/dickinsm/python_source/py3k/Lib/decimal.py", line 1487, in 
__round__
    return trunc(self.quantize(Decimal(1), rounding=ROUND_HALF_EVEN))
  File "/Users/dickinsm/python_source/py3k/Lib/decimal.py", line 2265, in 
quantize
    'quantize result has too many digits for current context')
  File "/Users/dickinsm/python_source/py3k/Lib/decimal.py", line 3546, in 
_raise_error
    raise error(explanation)
decimal.InvalidOperation: quantize result has too many digits for current 
context
>>> 

Can I suggest using _rescale instead of quantize?  For example,

    def __round__(self, ndigits:_numbers.Integral=None):
        """Rounds self to ndigits decimal places, defaulting to 0.

        If ndigits is omitted or None, returns an int, otherwise a
        Decimal. Rounds half toward even."""
        if ndigits is None:
            return trunc(self._rescale(0, rounding=ROUND_HALF_EVEN))
        return self._rescale(-ndigits, rounding=ROUND_HALF_EVEN)

__________________________________
Tracker <[EMAIL PROTECTED]>
<http://bugs.python.org/issue1623>
__________________________________
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to