Commit 74135d0a authored by Alexander Belopolsky's avatar Alexander Belopolsky

Made minimal modifications to pass included tests

parent 418182e1
...@@ -151,13 +151,9 @@ class Complex: ...@@ -151,13 +151,9 @@ class Complex:
raise ValueError("can't convert Complex with nonzero im to float") raise ValueError("can't convert Complex with nonzero im to float")
return float(self.re) return float(self.re)
def __cmp__(self, other): def __eq__(self, other):
other = ToComplex(other) other = ToComplex(other)
return cmp((self.re, self.im), (other.re, other.im)) return (self.re, self.im) == (other.re, other.im)
def __rcmp__(self, other):
other = ToComplex(other)
return cmp(other, self)
def __bool__(self): def __bool__(self):
return not (self.re == self.im == 0) return not (self.re == self.im == 0)
...@@ -190,14 +186,14 @@ class Complex: ...@@ -190,14 +186,14 @@ class Complex:
__rmul__ = __mul__ __rmul__ = __mul__
def __div__(self, other): def __truediv__(self, other):
other = ToComplex(other) other = ToComplex(other)
d = float(other.re*other.re + other.im*other.im) d = float(other.re*other.re + other.im*other.im)
if not d: raise ZeroDivisionError('Complex division') if not d: raise ZeroDivisionError('Complex division')
return Complex((self.re*other.re + self.im*other.im) / d, return Complex((self.re*other.re + self.im*other.im) / d,
(self.im*other.re - self.re*other.im) / d) (self.im*other.re - self.re*other.im) / d)
def __rdiv__(self, other): def __rtruediv__(self, other):
other = ToComplex(other) other = ToComplex(other)
return other / self return other / self
...@@ -226,8 +222,9 @@ def checkop(expr, a, b, value, fuzz = 1e-6): ...@@ -226,8 +222,9 @@ def checkop(expr, a, b, value, fuzz = 1e-6):
print(' ', a, 'and', b, end=' ') print(' ', a, 'and', b, end=' ')
try: try:
result = eval(expr) result = eval(expr)
except: except Exception as e:
result = sys.exc_info()[0] print('!!\t!!\t!! error: {}'.format(e))
return
print('->', result) print('->', result)
if isinstance(result, str) or isinstance(value, str): if isinstance(result, str) or isinstance(value, str):
ok = (result == value) ok = (result == value)
...@@ -295,13 +292,6 @@ def test(): ...@@ -295,13 +292,6 @@ def test():
(Complex(1), Complex(0,10), 1), (Complex(1), Complex(0,10), 1),
(2, Complex(4,0), 16), (2, Complex(4,0), 16),
], ],
'cmp(a,b)': [
(1, 10, -1),
(1, Complex(0,10), 1),
(Complex(0,10), 1, -1),
(Complex(0,10), Complex(1), -1),
(Complex(1), Complex(0,10), 1),
],
} }
for expr in sorted(testsuite): for expr in sorted(testsuite):
print(expr + ':') print(expr + ':')
......
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