bpo-32968: Make modulo and floor division involving Fraction and float consistent with other operations (#5956)

Make mixed-type `%` and `//` operations involving `Fraction` and `float` objects behave like all other mixed-type arithmetic operations: first the `Fraction` object is converted to a `float`, then the `float` operation is performed as normal. This fixes some surprising corner cases, like `Fraction('1/3') % inf` giving a NaN.

Thanks Elias Zamaria for the patch.
This commit is contained in:
Elias Zamaria 2018-08-26 23:59:28 -07:00 committed by Mark Dickinson
parent 74734f73ca
commit 393f1ff62e
4 changed files with 13 additions and 12 deletions

View file

@ -427,23 +427,18 @@ def _div(a, b):
__truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv)
def __floordiv__(a, b):
def _floordiv(a, b):
"""a // b"""
return math.floor(a / b)
def __rfloordiv__(b, a):
"""a // b"""
return math.floor(a / b)
__floordiv__, __rfloordiv__ = _operator_fallbacks(_floordiv, operator.floordiv)
def __mod__(a, b):
def _mod(a, b):
"""a % b"""
div = a // b
return a - b * div
def __rmod__(b, a):
"""a % b"""
div = a // b
return a - b * div
__mod__, __rmod__ = _operator_fallbacks(_mod, operator.mod)
def __pow__(a, b):
"""a ** b

View file

@ -401,15 +401,19 @@ def testMixedArithmetic(self):
self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10))
self.assertTypedEquals(0, F(1, 10) // 1)
self.assertTypedEquals(0, F(1, 10) // 1.0)
self.assertTypedEquals(0.0, F(1, 10) // 1.0)
self.assertTypedEquals(10, 1 // F(1, 10))
self.assertTypedEquals(10**23, 10**22 // F(1, 10))
self.assertTypedEquals(10, 1.0 // F(1, 10))
self.assertTypedEquals(1.0 // 0.1, 1.0 // F(1, 10))
self.assertTypedEquals(F(1, 10), F(1, 10) % 1)
self.assertTypedEquals(0.1, F(1, 10) % 1.0)
self.assertTypedEquals(F(0, 1), 1 % F(1, 10))
self.assertTypedEquals(0.0, 1.0 % F(1, 10))
self.assertTypedEquals(1.0 % 0.1, 1.0 % F(1, 10))
self.assertTypedEquals(0.1, F(1, 10) % float('inf'))
self.assertTypedEquals(float('-inf'), F(1, 10) % float('-inf'))
self.assertTypedEquals(float('inf'), F(-1, 10) % float('inf'))
self.assertTypedEquals(-0.1, F(-1, 10) % float('-inf'))
# No need for divmod since we don't override it.

View file

@ -1814,6 +1814,7 @@ Masazumi Yoshikawa
Arnaud Ysmal
Bernard Yue
Moshe Zadka
Elias Zamaria
Milan Zamazal
Artur Zaprzala
Mike Zarnstorff

View file

@ -0,0 +1 @@
Modulo and floor division involving Fraction and float should return float.