Message313045
Usually, a positive finite number modulo infinity is itself. But modding a positive fraction by infinity produces nan:
>>> from fractions import Fraction
>>> from math import inf
>>> 3 % inf
3.0
>>> 3.5 % inf
3.5
>>> Fraction('1/3') % inf
nan
Likewise, a positive number modulo negative infinity is usually negative infinity, a negative number modulo infinity is usually infinity, and a negative number modulo negative infinity is usually itself, unless the number doing the modding is a fraction, in which case it produces nan.
I think fractions should behave like other numbers in cases like these. I don't think this comes up very often in practical situations, but it is inconsistent behavior that may surprise people.
I looked at the fractions module. It seems like this can be fixed by putting the following lines at the top of the __mod__ method of the Fraction class:
if b == math.inf:
if a >= 0:
return a
else:
return math.inf
elif b == -math.inf:
if a >= 0:
return -math.inf
else:
return a
If that is too verbose, it can also be fixed with these lines, although this is less understandable IMO:
if math.isinf(b):
return a if (a >= 0) == (b > 0) else math.copysign(math.inf, b)
I noticed this in Python 3.6.4 on OS X 10.12.6. If anyone wants, I can come up with a patch with some tests. |
|
Date |
User |
Action |
Args |
2018-02-28 03:10:51 | elias | set | recipients:
+ elias |
2018-02-28 03:10:51 | elias | set | messageid: <1519787451.54.0.467229070634.issue32968@psf.upfronthosting.co.za> |
2018-02-28 03:10:51 | elias | link | issue32968 messages |
2018-02-28 03:10:50 | elias | create | |
|