New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement PEP-3141 for Decimal #45964
Comments
I added __round__, __ceil__, __floor__, and __trunc__ |
I think you probably don't want to use quantize here: it makes use of trunc(Decimal(integer_with_30_digits)) will fail with an InvalidOperation exception. I assume what's wanted is |
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.
|
Here's a version of the patch that uses _rescale instead of quantize. I |
Cool! Works for me. I agree that it's not 100% clear that round(large_decimal) should return an integer rather There is of course a problem here that's not present for floats, namely that someone can I notice that math.floor(large_float) and math.ceil(large_float) return floats at the One last thing: would it be worth backporting some of this to Python 2.6, just to avoid Mark |
Re math.{floor,ceil}(float): oops, that's definitely a bug. I'll fix it. Re backporting: yes, and I believe trunc() should be backported too. |
The PEP is not approved yet. This look interesting, will take a look Thanks for the work! Regards, |
Sorry, the PEP was approved, just not yet marked as such. :-) Will do |
I'm +1 to this change. As this does not negatively affect the behavior on Py2, for each part of
What do you think? |
BTW abc.py and _abcoll.py have been backported to 2.6 already, I propose |
Are you guys suggesting the backport before or after checking this in to |
If there aren't too many differences between the 2.6 and 3.0 version of Though you'd have to start by backporting *numbers.py first. |
Right. Will do. |
I think this reflects the consensus of The test currently fails due to bpo-1747. I'll double-check that it |
Much smaller now. 3.0 will need an additional patch beyond the |
One minor point: Decimals are also created by the _dec_from_triple |
Some more comments: (1) I don't think I understand the mechanism by which __lt__ and __le__ are supposed to Decimal(2) > 3 doesn't call __lt__ at any stage. Is this what's expected, or is this unintended? (2) Also on the subject of __le__ and __lt__: if Decimal is going to have rich (3) I'm getting a serious performance hit from this patch: a complete run of |
_dec_from_triple: Probably, yes. It doesn't seem to have any practical __lt__, __le__: It doesn't matter whether they're called because they NaNs: I think the guideline is to keep the current behavior for 2.6, and Performance: cProfile points at abc.__instancecheck__ taking 20% of the Thanks for your detailed reviews, Mark! |
Yes, making Decimal subclass object instead of Real and Inexact makes it I removed __le__ and __lt__ too, since, without subclassing |
The discussion on bpo-1682 concluded that Decimal should not implement |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: