Is an implementation of Python free to make up its own answers to division and modulus operatons in the case of inf and nan arguments?

The standard library documentation says only that // is "rounded towards minus infinity". The language reference says that ||:

1. |x == (x//y)*y + (x%y)|,
2. the modulus has the same sign as y, and
3. division by (either kind of) zero raises |ZeroDivisionError| .

It's consistent, but it doesn't define the operator over the full range of potential arguments. In Python 3.8 and 3.9:

>>> from decimal import Decimal
>>> Decimal('-3.14') // Decimal('Infinity')
Decimal('-0')
>>> -3.14 // float("inf")
-1.0
>>> import math
>>> math.floor(-3.14 /  float("inf"))
0

I can see sense in all three answers, as possible interpretations of "rounded towards minus infinity", but I quite like decimal's. There seem to be no regression tests for floor division of floats, and for modulus only with finite arguments, perhaps intentionally.

--

Jeff Allen

_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at 
https://mail.python.org/archives/list/python-dev@python.org/message/BXYBSUMNSP6AAAS6OL23ANSML4IOARVB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to