Kaydet (Commit) 909e334e authored tarafından Raymond Hettinger's avatar Raymond Hettinger

More code cleanup. Remove unnecessary indirection to useless class methods.

üst 31eaafef
...@@ -285,8 +285,9 @@ class Rational(RationalAbc): ...@@ -285,8 +285,9 @@ class Rational(RationalAbc):
__truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv) __truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv)
__div__, __rdiv__ = _operator_fallbacks(_div, operator.div) __div__, __rdiv__ = _operator_fallbacks(_div, operator.div)
@classmethod def __floordiv__(a, b):
def _floordiv(cls, a, b): """a // b"""
# Will be math.floor(a / b) in 3.0.
div = a / b div = a / b
if isinstance(div, RationalAbc): if isinstance(div, RationalAbc):
# trunc(math.floor(div)) doesn't work if the rational is # trunc(math.floor(div)) doesn't work if the rational is
...@@ -296,28 +297,27 @@ class Rational(RationalAbc): ...@@ -296,28 +297,27 @@ class Rational(RationalAbc):
else: else:
return math.floor(div) return math.floor(div)
def __floordiv__(a, b):
"""a // b"""
# Will be math.floor(a / b) in 3.0.
return a._floordiv(a, b)
def __rfloordiv__(b, a): def __rfloordiv__(b, a):
"""a // b""" """a // b"""
# Will be math.floor(a / b) in 3.0. # Will be math.floor(a / b) in 3.0.
return b._floordiv(a, b) div = a / b
if isinstance(div, RationalAbc):
@classmethod # trunc(math.floor(div)) doesn't work if the rational is
def _mod(cls, a, b): # more precise than a float because the intermediate
div = a // b # rounding may cross an integer boundary.
return a - b * div return div.numerator // div.denominator
else:
return math.floor(div)
def __mod__(a, b): def __mod__(a, b):
"""a % b""" """a % b"""
return a._mod(a, b) div = a // b
return a - b * div
def __rmod__(b, a): def __rmod__(b, a):
"""a % b""" """a % b"""
return b._mod(a, b) div = a // b
return a - b * div
def __pow__(a, b): def __pow__(a, b):
"""a ** b """a ** b
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment