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.

Author mark.dickinson
Recipients facundobatista, jyasskin, mark.dickinson
Date 2007-12-14.15:17:46
SpamBayes Score 0.017421233
Marked as misclassified No
Message-id <1197645466.85.0.699069525464.issue1623@psf.upfronthosting.co.za>
In-reply-to
Content
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)
History
Date User Action Args
2007-12-14 15:17:47mark.dickinsonsetspambayes_score: 0.0174212 -> 0.017421233
recipients: + mark.dickinson, facundobatista, jyasskin
2007-12-14 15:17:46mark.dickinsonsetspambayes_score: 0.0174212 -> 0.0174212
messageid: <1197645466.85.0.699069525464.issue1623@psf.upfronthosting.co.za>
2007-12-14 15:17:46mark.dickinsonlinkissue1623 messages
2007-12-14 15:17:46mark.dickinsoncreate