[issue22306] Inconsistent division by 0 behaviour in decimal module
Raymond Hettinger added the comment: Sorry. Scratch my last comment. I see from the docs ( https://docs.python.org/3/library/decimal.html ) vthat the decimal module explicitly references that IBM spec. I imagine that standard python arithmatic doesn't even attempt to conform to this ibm spec. That is exactly correct. Closing as not-a-bug. -- resolution: - not a bug status: open - closed ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Changes by Jesús Cea Avión j...@jcea.es: -- nosy: +jcea ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
New submission from Akima: 1 / 0 (where both numbers are decimal.Decimal) produces a decimal.DivisionByZero exception as I would expect. This is useful. I can use a simple try except block to catch a potential division by zero error in my code. 0 / 0 (where both numbers are decimal.Decimal) produces a decimal.InvalidOperation exception. This is undesirable. I would expect another decimal.DivisionByZero exception. This means that if I want to catch a division by zero error in my code using a try except block, I now have to catch exceptions for both decimal.DivisionByZero and decimal.InvalidOperation. Presumably decimal.InvalidOperation can be raised in other scenarios, so catching it may result in masking a programming fault (which isn't just a division by zero: 0 / 0). If you perform the same division but using standard Python integers instead of decimal.Decimal objects, the behaviour is exactly as you would expect: 0 / 0 and 1 / 0 both produce a ZeroDivisionError exception. I have tested this in CPython 3.3.5, 3.2.3 and 2.7.3. All versions produce the same behaviour. Demonstration: Python 2.7.3 (default, Feb 27 2014, 19:58:35) [GCC 4.6.3] on linux2 Type help, copyright, credits or license for more information. from decimal import Decimal as d d(1) / d(0) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python2.7/decimal.py, line 1323, in __truediv__ return context._raise_error(DivisionByZero, 'x / 0', sign) File /usr/lib/python2.7/decimal.py, line 3866, in _raise_error raise error(explanation) decimal.DivisionByZero: x / 0 d(0) / d(0) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python2.7/decimal.py, line 1322, in __truediv__ return context._raise_error(DivisionUndefined, '0 / 0') File /usr/lib/python2.7/decimal.py, line 3866, in _raise_error raise error(explanation) decimal.InvalidOperation: 0 / 0 1 / 0 Traceback (most recent call last): File stdin, line 1, in module ZeroDivisionError: integer division or modulo by zero 0 / 0 Traceback (most recent call last): File stdin, line 1, in module ZeroDivisionError: integer division or modulo by zero Here is the same demonstration but using a Python 3.2.3 interpreter: Python 3.2.3 (default, Feb 27 2014, 21:31:18) [GCC 4.6.3] on linux2 Type help, copyright, credits or license for more information. from decimal import Decimal as d d(1) / d(0) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python3.2/decimal.py, line 1300, in __truediv__ return context._raise_error(DivisionByZero, 'x / 0', sign) File /usr/lib/python3.2/decimal.py, line 3926, in _raise_error raise error(explanation) decimal.DivisionByZero: x / 0 d(0) / d(0) Traceback (most recent call last): File stdin, line 1, in module File /usr/lib/python3.2/decimal.py, line 1299, in __truediv__ return context._raise_error(DivisionUndefined, '0 / 0') File /usr/lib/python3.2/decimal.py, line 3926, in _raise_error raise error(explanation) decimal.InvalidOperation: 0 / 0 1 / 0 Traceback (most recent call last): File stdin, line 1, in module ZeroDivisionError: division by zero 0 / 0 Traceback (most recent call last): File stdin, line 1, in module ZeroDivisionError: division by zero -- messages: 226125 nosy: akima priority: normal severity: normal status: open title: Inconsistent division by 0 behaviour in decimal module type: behavior versions: Python 2.7, Python 3.2, Python 3.3 ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Changes by Akima m...@aki.ma: -- components: +Library (Lib) ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Changes by Antoine Pitrou pit...@free.fr: -- nosy: +skrah ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Changes by Antoine Pitrou pit...@free.fr: -- nosy: +facundobatista, mark.dickinson, rhettinger ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Stefan Krah added the comment: The behavior is according to the specification: http://speleotrove.com/decimal/decarith.html The idea behind it is that 1/0 can be reasonably defined as infinity, whereas 0/0 is undefined. You can see that if you disable the exceptions: c = getcontext() c.traps[DivisionByZero] = False c.traps[InvalidOperation] = False Decimal(1) / 0 Decimal('Infinity') Decimal(0) / 0 Decimal('NaN') -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Akima added the comment: Hi skrah. Thanks for the feedback. That specification is interesting. As this IBM spec appears to be a /general/ specification for performing decimal arithmatic and not targetted specifically at Python's decimal arithmatic implementation, I would expect all of Python to adhere to its recommendations (for consitency). If the division by 0 behaviour of the decimal module is in fact correct (as per the spec you have linked) and desirable, then perhaps the Python standard integer division by zero behaviour is incorrect. 0 / 0 ... raises a ZeroDivisionError exception. This is in conflict with the IBM spec and with the behaviour of the decimal module. (I realize that arithmatic in the decimal module is not supposed to be equivalent to arithmatic with standard python number types, but this exception behaviour seems like something that should be consistent between the two arithmatic implementations.) -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Akima added the comment: Sorry. Scratch my last comment. I see from the docs ( https://docs.python.org/3/library/decimal.html ) that the decimal module explicitly references that IBM spec. I imagine that standard python arithmatic doesn't even attempt to conform to this ibm spec. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue22306] Inconsistent division by 0 behaviour in decimal module
Stefan Krah added the comment: According to IEEE 754-2008 binary floats should use the same exceptions in this case. 7.2 Invalid operation ... e) division: division(0, 0) or division(∞, ∞) 7.3 Division by zero The divideByZero exception shall be signaled if and only if an exact infinite result is defined for an operation on finite operands. But the Python binary float implementation is a lot older than the 2008 standard. -- ___ Python tracker rep...@bugs.python.org http://bugs.python.org/issue22306 ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com