Get rid of remnants of integer division

This commit is contained in:
Neal Norwitz 2006-03-24 08:14:36 +00:00
parent ed483ba63b
commit bcc0db82dc
28 changed files with 47 additions and 212 deletions

View file

@ -1135,10 +1135,9 @@ class Decimal(object):
return ans
__rmul__ = __mul__
def __div__(self, other, context=None):
def __truediv__(self, other, context=None):
"""Return self / other."""
return self._divide(other, context=context)
__truediv__ = __div__
def _divide(self, other, divmod = 0, context=None):
"""Return a / b, to context.prec precision.
@ -1306,13 +1305,12 @@ class Decimal(object):
ans = ans._fix(context)
return ans
def __rdiv__(self, other, context=None):
"""Swaps self/other and returns __div__."""
def __rtruediv__(self, other, context=None):
"""Swaps self/other and returns __truediv__."""
other = _convert_other(other)
if other is NotImplemented:
return other
return other.__div__(self, context=context)
__rtruediv__ = __rdiv__
return other.__truediv__(self, context=context)
def __divmod__(self, other, context=None):
"""
@ -1384,9 +1382,9 @@ class Decimal(object):
rounding = context._set_rounding_decision(NEVER_ROUND)
if other._sign:
comparison = other.__div__(Decimal(-2), context=context)
comparison = other.__truediv__(Decimal(-2), context=context)
else:
comparison = other.__div__(Decimal(2), context=context)
comparison = other.__truediv__(Decimal(2), context=context)
context._set_rounding_decision(rounding)
context._regard_flags(*flags)
@ -1751,7 +1749,7 @@ class Decimal(object):
if n < 0:
#n is a long now, not Decimal instance
n = -n
mul = Decimal(1).__div__(mul, context=context)
mul = Decimal(1).__truediv__(mul, context=context)
spot = 1
while spot <= n:
@ -1972,7 +1970,7 @@ class Decimal(object):
rounding = context._set_rounding(ROUND_HALF_EVEN)
while 1:
context.prec = min(2*context.prec - 2, maxp)
ans = half.__mul__(ans.__add__(tmp.__div__(ans, context=context),
ans = half.__mul__(ans.__add__(tmp.__truediv__(ans, context=context),
context=context), context=context)
if context.prec == maxp:
break
@ -2454,7 +2452,7 @@ class Context(object):
>>> ExtendedContext.divide(Decimal('2.40E+6'), Decimal('2'))
Decimal("1.20E+6")
"""
return a.__div__(b, context=self)
return a.__truediv__(b, context=self)
def divide_int(self, a, b):
"""Divides two numbers and returns the integer part of the result.

View file

@ -5,7 +5,7 @@ x += 1
x *= 2
x **= 2
x -= 8
x //= 2
x /= 2
x //= 1
x %= 12
x &= 2
@ -19,7 +19,7 @@ x[0] += 1
x[0] *= 2
x[0] **= 2
x[0] -= 8
x[0] //= 2
x[0] /= 2
x[0] //= 2
x[0] %= 12
x[0] &= 2
@ -33,7 +33,7 @@ x[0] += 1
x[0] *= 2
x[0] **= 2
x[0] -= 8
x[0] //= 2
x[0] /= 2
x[0] //= 1
x[0] %= 12
x[0] &= 2
@ -123,14 +123,6 @@ class testall:
print "__imul__ called"
return self
def __div__(self, val):
print "__div__ called"
def __rdiv__(self, val):
print "__rdiv__ called"
def __idiv__(self, val):
print "__idiv__ called"
return self
def __floordiv__(self, val):
print "__floordiv__ called"
return self
@ -147,6 +139,9 @@ class testall:
def __itruediv__(self, val):
print "__itruediv__ called"
return self
def __rtruediv__(self, val):
print "__rtruediv__ called"
return self
def __mod__(self, val):
print "__mod__ called"
@ -217,16 +212,9 @@ x * 1
1 * x
x *= 1
if 1/2 == 0:
x / 1
1 / x
x /= 1
else:
# True division is in effect, so "/" doesn't map to __div__ etc;
# but the canned expected-output file requires that those get called.
x.__div__(1)
x.__rdiv__(1)
x.__idiv__(1)
x / 1
1 / x
x /= 1
x // 1
1 // x

View file

@ -140,8 +140,6 @@ class Rat(object):
return float(self) / other
return NotImplemented
__div__ = __truediv__
def __rtruediv__(self, other):
"""Divide two Rats, or a Rat and a number (reversed args)."""
if isRat(other):
@ -152,8 +150,6 @@ class Rat(object):
return other / float(self)
return NotImplemented
__rdiv__ = __rtruediv__
def __floordiv__(self, other):
"""Divide two Rats, returning the floored result."""
if isint(other):

View file

@ -11,8 +11,8 @@ testmeths = [
"rsub",
"mul",
"rmul",
"div",
"rdiv",
"truediv",
"rtruediv",
"mod",
"rmod",
"divmod",
@ -134,16 +134,8 @@ testme - 1
testme * 1
1 * testme
if 1/2 == 0:
testme / 1
1 / testme
else:
# True division is in effect, so "/" doesn't map to __div__ etc; but
# the canned expected-output file requires that __div__ etc get called.
testme.__coerce__(1)
testme.__div__(1)
testme.__coerce__(1)
testme.__rdiv__(1)
testme / 1
1 / testme
testme % 1
1 % testme

View file

@ -44,10 +44,10 @@ class MethodNumber:
def __rmul__(self,other):
return other * self.arg
def __div__(self,other):
def __truediv__(self,other):
return self.arg / other
def __rdiv__(self,other):
def __rtruediv__(self,other):
return other / self.arg
def __pow__(self,other):

View file

@ -55,19 +55,15 @@ class ComplexTest(unittest.TestCase):
if x != 0:
q = z / x
self.assertClose(q, y)
q = z.__div__(x)
self.assertClose(q, y)
q = z.__truediv__(x)
self.assertClose(q, y)
if y != 0:
q = z / y
self.assertClose(q, x)
q = z.__div__(y)
self.assertClose(q, x)
q = z.__truediv__(y)
self.assertClose(q, x)
def test_div(self):
def test_truediv(self):
simple_real = [float(i) for i in xrange(-5, 6)]
simple_complex = [complex(x, y) for x in simple_real for y in simple_real]
for x in simple_complex:
@ -84,7 +80,7 @@ class ComplexTest(unittest.TestCase):
self.check_div(complex(random(), random()),
complex(random(), random()))
self.assertRaises(ZeroDivisionError, complex.__div__, 1+1j, 0+0j)
self.assertRaises(ZeroDivisionError, complex.__truediv__, 1+1j, 0+0j)
# FIXME: The following currently crashes on Alpha
# self.assertRaises(OverflowError, pow, 1e200+1j, 1e200+1j)

View file

@ -507,7 +507,7 @@ class DecimalImplicitConstructionTest(unittest.TestCase):
('+', '__add__', '__radd__'),
('-', '__sub__', '__rsub__'),
('*', '__mul__', '__rmul__'),
('/', '__div__', '__rdiv__'),
('/', '__truediv__', '__rtruediv__'),
('%', '__mod__', '__rmod__'),
('//', '__floordiv__', '__rfloordiv__'),
('**', '__pow__', '__rpow__'),
@ -975,7 +975,6 @@ class DecimalUsabilityTest(unittest.TestCase):
checkSameDec("__abs__")
checkSameDec("__add__", True)
checkSameDec("__div__", True)
checkSameDec("__divmod__", True)
checkSameDec("__cmp__", True)
checkSameDec("__float__")
@ -990,7 +989,6 @@ class DecimalUsabilityTest(unittest.TestCase):
checkSameDec("__pos__")
checkSameDec("__pow__", True)
checkSameDec("__radd__", True)
checkSameDec("__rdiv__", True)
checkSameDec("__rdivmod__", True)
checkSameDec("__repr__")
checkSameDec("__rfloordiv__", True)

View file

@ -29,10 +29,6 @@ def testbinop(a, b, res, expr="a+b", meth="__add__"):
if verbose: print "checking", expr
dict = {'a': a, 'b': b}
# XXX Hack so this passes before 2.3 when -Qnew is specified.
if meth == "__div__" and 1/2 == 0.5:
meth = "__truediv__"
vereq(eval(expr, dict), res)
t = type(a)
m = getattr(t, meth)
@ -4044,9 +4040,8 @@ def notimplemented():
('__add__', 'x + y', 'x += y'),
('__sub__', 'x - y', 'x -= y'),
('__mul__', 'x * y', 'x *= y'),
('__truediv__', 'operator.truediv(x, y)', None),
('__floordiv__', 'operator.floordiv(x, y)', None),
('__div__', 'x / y', 'x /= y'),
('__truediv__', 'x / y', None),
('__floordiv__', 'x // y', None),
('__mod__', 'x % y', 'x %= y'),
('__divmod__', 'divmod(x, y)', None),
('__pow__', 'x ** y', 'x **= y'),

View file

@ -144,11 +144,6 @@ class OperatorTestCase(unittest.TestCase):
self.failUnless(operator.delslice(a, 2, 8) is None)
self.assert_(a == [0, 1, 8, 9])
def test_div(self):
self.failUnlessRaises(TypeError, operator.div, 5)
self.failUnlessRaises(TypeError, operator.div, None, None)
self.failUnless(operator.floordiv(5, 2) == 2)
def test_floordiv(self):
self.failUnlessRaises(TypeError, operator.floordiv, 5)
self.failUnlessRaises(TypeError, operator.floordiv, None, None)
@ -416,7 +411,6 @@ class OperatorTestCase(unittest.TestCase):
class C(object):
def __iadd__ (self, other): return "iadd"
def __iand__ (self, other): return "iand"
def __idiv__ (self, other): return "idiv"
def __ifloordiv__(self, other): return "ifloordiv"
def __ilshift__ (self, other): return "ilshift"
def __imod__ (self, other): return "imod"
@ -431,7 +425,6 @@ class OperatorTestCase(unittest.TestCase):
c = C()
self.assertEqual(operator.iadd (c, 5), "iadd")
self.assertEqual(operator.iand (c, 5), "iand")
self.assertEqual(operator.idiv (c, 5), "idiv")
self.assertEqual(operator.ifloordiv(c, 5), "ifloordiv")
self.assertEqual(operator.ilshift (c, 5), "ilshift")
self.assertEqual(operator.imod (c, 5), "imod")
@ -446,7 +439,6 @@ class OperatorTestCase(unittest.TestCase):
self.assertEqual(operator.irepeat (c, 5), "imul")
self.assertEqual(operator.__iadd__ (c, 5), "iadd")
self.assertEqual(operator.__iand__ (c, 5), "iand")
self.assertEqual(operator.__idiv__ (c, 5), "idiv")
self.assertEqual(operator.__ifloordiv__(c, 5), "ifloordiv")
self.assertEqual(operator.__ilshift__ (c, 5), "ilshift")
self.assertEqual(operator.__imod__ (c, 5), "imod")