Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9893de1

Browse files
committed
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().
1 parent 3564146 commit 9893de1

3 files changed

Lines changed: 20 additions & 53 deletions

File tree

Lib/numbers.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
66
TODO: Fill out more detailed documentation on the operators."""
77

8-
from __future__ import division
98
from abc import ABCMeta, abstractmethod, abstractproperty
109

1110
__all__ = ["Number", "Exact", "Inexact",
@@ -62,8 +61,7 @@ class Complex(Number):
6261
def __complex__(self):
6362
"""Return a builtin complex instance. Called for complex(self)."""
6463

65-
# Will be __bool__ in 3.0.
66-
def __nonzero__(self):
64+
def __bool__(self):
6765
"""True if self != 0. Called for bool(self)."""
6866
return self != 0
6967

@@ -121,30 +119,14 @@ def __rmul__(self, other):
121119
"""other * self"""
122120
raise NotImplementedError
123121

124-
@abstractmethod
125-
def __div__(self, other):
126-
"""self / other without __future__ division
127-
128-
May promote to float.
129-
"""
130-
raise NotImplementedError
131-
132-
@abstractmethod
133-
def __rdiv__(self, other):
134-
"""other / self without __future__ division"""
135-
raise NotImplementedError
136-
137122
@abstractmethod
138123
def __truediv__(self, other):
139-
"""self / other with __future__ division.
140-
141-
Should promote to float when necessary.
142-
"""
124+
"""self / other: Should promote to float when necessary."""
143125
raise NotImplementedError
144126

145127
@abstractmethod
146128
def __rtruediv__(self, other):
147-
"""other / self with __future__ division"""
129+
"""other / self"""
148130
raise NotImplementedError
149131

150132
@abstractmethod

Lib/rational.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
"""Rational, infinite-precision, real numbers."""
55

6-
from __future__ import division
76
import math
87
import numbers
98
import operator
@@ -203,28 +202,14 @@ def _div(a, b):
203202
a.denominator * b.numerator)
204203

205204
__truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv)
206-
__div__, __rdiv__ = _operator_fallbacks(_div, operator.truediv)
207-
208-
@classmethod
209-
def _floordiv(cls, a, b):
210-
div = a / b
211-
if isinstance(div, RationalAbc):
212-
# trunc(math.floor(div)) doesn't work if the rational is
213-
# more precise than a float because the intermediate
214-
# rounding may cross an integer boundary.
215-
return div.numerator // div.denominator
216-
else:
217-
return math.floor(div)
218205

219206
def __floordiv__(a, b):
220207
"""a // b"""
221-
# Will be math.floor(a / b) in 3.0.
222-
return a._floordiv(a, b)
208+
return math.floor(a / b)
223209

224210
def __rfloordiv__(b, a):
225211
"""a // b"""
226-
# Will be math.floor(a / b) in 3.0.
227-
return b._floordiv(a, b)
212+
return math.floor(a / b)
228213

229214
@classmethod
230215
def _mod(cls, a, b):
@@ -324,11 +309,11 @@ def __round__(self, ndigits=None):
324309
shift = 10**abs(ndigits)
325310
# See _operator_fallbacks.forward to check that the results of
326311
# these operations will always be Rational and therefore have
327-
# __round__().
312+
# round().
328313
if ndigits > 0:
329-
return Rational((self * shift).__round__(), shift)
314+
return Rational(round(self * shift), shift)
330315
else:
331-
return Rational((self / shift).__round__() * shift)
316+
return Rational(round(self / shift) * shift)
332317

333318
def __hash__(self):
334319
"""hash(self)

Lib/test/test_rational.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ def testFromFloat(self):
7474

7575
def testConversions(self):
7676
self.assertTypedEquals(-1, trunc(R(-11, 10)))
77-
self.assertTypedEquals(-2, R(-11, 10).__floor__())
78-
self.assertTypedEquals(-1, R(-11, 10).__ceil__())
79-
self.assertTypedEquals(-1, R(-10, 10).__ceil__())
77+
self.assertTypedEquals(-2, math.floor(R(-11, 10)))
78+
self.assertTypedEquals(-1, math.ceil(R(-11, 10)))
79+
self.assertTypedEquals(-1, math.ceil(R(-10, 10)))
8080

81-
self.assertTypedEquals(0, R(-1, 10).__round__())
82-
self.assertTypedEquals(0, R(-5, 10).__round__())
83-
self.assertTypedEquals(-2, R(-15, 10).__round__())
84-
self.assertTypedEquals(-1, R(-7, 10).__round__())
81+
self.assertTypedEquals(0, round(R(-1, 10)))
82+
self.assertTypedEquals(0, round(R(-5, 10)))
83+
self.assertTypedEquals(-2, round(R(-15, 10)))
84+
self.assertTypedEquals(-1, round(R(-7, 10)))
8585

8686
self.assertEquals(False, bool(R(0, 1)))
8787
self.assertEquals(True, bool(R(3, 2)))
@@ -96,11 +96,11 @@ def testConversions(self):
9696
self.assertTypedEquals(0.1+0j, complex(R(1,10)))
9797

9898
def testRound(self):
99-
self.assertTypedEquals(R(-200), R(-150).__round__(-2))
100-
self.assertTypedEquals(R(-200), R(-250).__round__(-2))
101-
self.assertTypedEquals(R(30), R(26).__round__(-1))
102-
self.assertTypedEquals(R(-2, 10), R(-15, 100).__round__(1))
103-
self.assertTypedEquals(R(-2, 10), R(-25, 100).__round__(1))
99+
self.assertTypedEquals(R(-200), round(R(-150), -2))
100+
self.assertTypedEquals(R(-200), round(R(-250), -2))
101+
self.assertTypedEquals(R(30), round(R(26), -1))
102+
self.assertTypedEquals(R(-2, 10), round(R(-15, 100), 1))
103+
self.assertTypedEquals(R(-2, 10), round(R(-25, 100), 1))
104104

105105

106106
def testArithmetic(self):

0 commit comments

Comments
 (0)