diff options
author | Jeffrey Yasskin <jyasskin@gmail.com> | 2008-01-17 07:36:30 (GMT) |
---|---|---|
committer | Jeffrey Yasskin <jyasskin@gmail.com> | 2008-01-17 07:36:30 (GMT) |
commit | 9893de1b83978249eafaebe575989537c01b484e (patch) | |
tree | 25f465a8eba333743569badfb2783e13c8a8b2f8 | |
parent | 35641461db7c692877ffa4bbbfe31a525c81770e (diff) | |
download | cpython-9893de1b83978249eafaebe575989537c01b484e.zip cpython-9893de1b83978249eafaebe575989537c01b484e.tar.gz cpython-9893de1b83978249eafaebe575989537c01b484e.tar.bz2 |
Update the py3k version of the rational module to expose only methods needed by
py3k (i.e., no __div__) and use py3k functions like math.floor().
-rw-r--r-- | Lib/numbers.py | 24 | ||||
-rwxr-xr-x | Lib/rational.py | 25 | ||||
-rw-r--r-- | Lib/test/test_rational.py | 24 |
3 files changed, 20 insertions, 53 deletions
diff --git a/Lib/numbers.py b/Lib/numbers.py index 7c73fa7..6c3c3e1 100644 --- a/Lib/numbers.py +++ b/Lib/numbers.py @@ -5,7 +5,6 @@ TODO: Fill out more detailed documentation on the operators.""" -from __future__ import division from abc import ABCMeta, abstractmethod, abstractproperty __all__ = ["Number", "Exact", "Inexact", @@ -62,8 +61,7 @@ class Complex(Number): def __complex__(self): """Return a builtin complex instance. Called for complex(self).""" - # Will be __bool__ in 3.0. - def __nonzero__(self): + def __bool__(self): """True if self != 0. Called for bool(self).""" return self != 0 @@ -122,29 +120,13 @@ class Complex(Number): raise NotImplementedError @abstractmethod - def __div__(self, other): - """self / other without __future__ division - - May promote to float. - """ - raise NotImplementedError - - @abstractmethod - def __rdiv__(self, other): - """other / self without __future__ division""" - raise NotImplementedError - - @abstractmethod def __truediv__(self, other): - """self / other with __future__ division. - - Should promote to float when necessary. - """ + """self / other: Should promote to float when necessary.""" raise NotImplementedError @abstractmethod def __rtruediv__(self, other): - """other / self with __future__ division""" + """other / self""" raise NotImplementedError @abstractmethod diff --git a/Lib/rational.py b/Lib/rational.py index bece207..c913ec7 100755 --- a/Lib/rational.py +++ b/Lib/rational.py @@ -3,7 +3,6 @@ """Rational, infinite-precision, real numbers.""" -from __future__ import division import math import numbers import operator @@ -203,28 +202,14 @@ class Rational(RationalAbc): a.denominator * b.numerator) __truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv) - __div__, __rdiv__ = _operator_fallbacks(_div, operator.truediv) - - @classmethod - def _floordiv(cls, a, b): - 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 __floordiv__(a, b): """a // b""" - # Will be math.floor(a / b) in 3.0. - return a._floordiv(a, b) + return math.floor(a / b) def __rfloordiv__(b, a): """a // b""" - # Will be math.floor(a / b) in 3.0. - return b._floordiv(a, b) + return math.floor(a / b) @classmethod def _mod(cls, a, b): @@ -324,11 +309,11 @@ class Rational(RationalAbc): shift = 10**abs(ndigits) # See _operator_fallbacks.forward to check that the results of # these operations will always be Rational and therefore have - # __round__(). + # round(). if ndigits > 0: - return Rational((self * shift).__round__(), shift) + return Rational(round(self * shift), shift) else: - return Rational((self / shift).__round__() * shift) + return Rational(round(self / shift) * shift) def __hash__(self): """hash(self) diff --git a/Lib/test/test_rational.py b/Lib/test/test_rational.py index cd8c709..952a97f 100644 --- a/Lib/test/test_rational.py +++ b/Lib/test/test_rational.py @@ -74,14 +74,14 @@ class RationalTest(unittest.TestCase): def testConversions(self): self.assertTypedEquals(-1, trunc(R(-11, 10))) - self.assertTypedEquals(-2, R(-11, 10).__floor__()) - self.assertTypedEquals(-1, R(-11, 10).__ceil__()) - self.assertTypedEquals(-1, R(-10, 10).__ceil__()) + self.assertTypedEquals(-2, math.floor(R(-11, 10))) + self.assertTypedEquals(-1, math.ceil(R(-11, 10))) + self.assertTypedEquals(-1, math.ceil(R(-10, 10))) - self.assertTypedEquals(0, R(-1, 10).__round__()) - self.assertTypedEquals(0, R(-5, 10).__round__()) - self.assertTypedEquals(-2, R(-15, 10).__round__()) - self.assertTypedEquals(-1, R(-7, 10).__round__()) + self.assertTypedEquals(0, round(R(-1, 10))) + self.assertTypedEquals(0, round(R(-5, 10))) + self.assertTypedEquals(-2, round(R(-15, 10))) + self.assertTypedEquals(-1, round(R(-7, 10))) self.assertEquals(False, bool(R(0, 1))) self.assertEquals(True, bool(R(3, 2))) @@ -96,11 +96,11 @@ class RationalTest(unittest.TestCase): self.assertTypedEquals(0.1+0j, complex(R(1,10))) def testRound(self): - self.assertTypedEquals(R(-200), R(-150).__round__(-2)) - self.assertTypedEquals(R(-200), R(-250).__round__(-2)) - self.assertTypedEquals(R(30), R(26).__round__(-1)) - self.assertTypedEquals(R(-2, 10), R(-15, 100).__round__(1)) - self.assertTypedEquals(R(-2, 10), R(-25, 100).__round__(1)) + self.assertTypedEquals(R(-200), round(R(-150), -2)) + self.assertTypedEquals(R(-200), round(R(-250), -2)) + self.assertTypedEquals(R(30), round(R(26), -1)) + self.assertTypedEquals(R(-2, 10), round(R(-15, 100), 1)) + self.assertTypedEquals(R(-2, 10), round(R(-25, 100), 1)) def testArithmetic(self): |