[Python-checkins] r60269 - python/trunk/Lib/rational.py
raymond.hettinger
python-checkins at python.org
Fri Jan 25 00:50:26 CET 2008
Author: raymond.hettinger
Date: Fri Jan 25 00:50:26 2008
New Revision: 60269
Modified:
python/trunk/Lib/rational.py
Log:
More code cleanup. Remove unnecessary indirection to useless class methods.
Modified: python/trunk/Lib/rational.py
==============================================================================
--- python/trunk/Lib/rational.py (original)
+++ python/trunk/Lib/rational.py Fri Jan 25 00:50:26 2008
@@ -285,8 +285,9 @@
__truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv)
__div__, __rdiv__ = _operator_fallbacks(_div, operator.div)
- @classmethod
- def _floordiv(cls, a, b):
+ def __floordiv__(a, b):
+ """a // b"""
+ # Will be math.floor(a / b) in 3.0.
div = a / b
if isinstance(div, RationalAbc):
# trunc(math.floor(div)) doesn't work if the rational is
@@ -296,28 +297,27 @@
else:
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):
"""a // b"""
# Will be math.floor(a / b) in 3.0.
- return b._floordiv(a, b)
-
- @classmethod
- def _mod(cls, a, b):
- div = a // b
- return a - b * div
+ div = a / b
+ if isinstance(div, RationalAbc):
+ # trunc(math.floor(div)) doesn't work if the rational is
+ # more precise than a float because the intermediate
+ # rounding may cross an integer boundary.
+ return div.numerator // div.denominator
+ else:
+ return math.floor(div)
def __mod__(a, b):
"""a % b"""
- return a._mod(a, b)
+ div = a // b
+ return a - b * div
def __rmod__(b, a):
"""a % b"""
- return b._mod(a, b)
+ div = a // b
+ return a - b * div
def __pow__(a, b):
"""a ** b
More information about the Python-checkins
mailing list