summaryrefslogtreecommitdiffstats
path: root/Lib/fractions.py
diff options
context:
space:
mode:
authorElias Zamaria <mikez302@gmail.com>2018-08-27 06:59:28 (GMT)
committerMark Dickinson <mdickinson@enthought.com>2018-08-27 06:59:28 (GMT)
commit393f1ff62e032f20adaed2613a9e2d2d6bcb1eb3 (patch)
tree4fa1002c40971a5352722a49a64ed1ae1415f40a /Lib/fractions.py
parent74734f73ca3cdb48d4d51139512b6828c2734252 (diff)
downloadcpython-393f1ff62e032f20adaed2613a9e2d2d6bcb1eb3.zip
cpython-393f1ff62e032f20adaed2613a9e2d2d6bcb1eb3.tar.gz
cpython-393f1ff62e032f20adaed2613a9e2d2d6bcb1eb3.tar.bz2
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.
Diffstat (limited to 'Lib/fractions.py')
-rw-r--r--Lib/fractions.py13
1 files changed, 4 insertions, 9 deletions
diff --git a/Lib/fractions.py b/Lib/fractions.py
index 8330202..e0a024a 100644
--- a/Lib/fractions.py
+++ b/Lib/fractions.py
@@ -427,23 +427,18 @@ class Fraction(numbers.Rational):
__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