8000 bpo-32968: Modulo and floor division involving Fraction and float should return float by elias6 · Pull Request #5956 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

bpo-32968: Modulo and floor division involving Fraction and float should return float #5956

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

Merged
merged 10 commits into from
Aug 27, 2018
13 changes: 4 additions & 9 deletions Lib/fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 7 additions & 3 deletions Lib/test/test_fractions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1759,6 +1759,7 @@ Masazumi Yoshikawa
Arnaud Ysmal
Bernard Yue
Moshe Zadka
Elias Zamaria
Milan Zamazal
Artur Zaprzala
Mike Zarnstorff
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Modulo and floor division involving Fraction and float should return float.
0