Kaydet (Commit) bdaad8c9 authored tarafından Raymond Hettinger's avatar Raymond Hettinger

* Fix error in definition of Im() which returned self instead of 0

  for non-complex arguments.

* Replace type() comparisons with isinstance() checks.

* Replace apply() calls with equivalent syntactic form.

* Simplify __hash__ definition to hash the underlying tuple.

* Use math.hypot() for more robust computation of __abs__().

* Use sorted() instead of the multi-step keys/sort/iter.

* Update comment on the cmath module.
üst ab9ec165
...@@ -54,7 +54,7 @@ ...@@ -54,7 +54,7 @@
# nor are shift and mask operations. # nor are shift and mask operations.
# #
# The standard module math does not support complex numbers. # The standard module math does not support complex numbers.
# (I suppose it would be easy to implement a cmath module.) # The cmath modules should be used instead.
# #
# Idea: # Idea:
# add a class Polar(r, phi) and mixed-mode arithmetic which # add a class Polar(r, phi) and mixed-mode arithmetic which
...@@ -62,7 +62,7 @@ ...@@ -62,7 +62,7 @@
# Complex for +,-,cmp # Complex for +,-,cmp
# Polar for *,/,pow # Polar for *,/,pow
import types, math import math
import sys import sys
twopi = math.pi*2.0 twopi = math.pi*2.0
...@@ -74,8 +74,8 @@ def IsComplex(obj): ...@@ -74,8 +74,8 @@ def IsComplex(obj):
def ToComplex(obj): def ToComplex(obj):
if IsComplex(obj): if IsComplex(obj):
return obj return obj
elif type(obj) == types.TupleType: elif isinstance(obj, tuple):
return apply(Complex, obj) return Complex(*obj)
else: else:
return Complex(obj) return Complex(obj)
...@@ -86,14 +86,12 @@ def PolarToComplex(r = 0, phi = 0, fullcircle = twopi): ...@@ -86,14 +86,12 @@ def PolarToComplex(r = 0, phi = 0, fullcircle = twopi):
def Re(obj): def Re(obj):
if IsComplex(obj): if IsComplex(obj):
return obj.re return obj.re
else: return obj
return obj
def Im(obj): def Im(obj):
if IsComplex(obj): if IsComplex(obj):
return obj.im return obj.im
else: return 0
return obj
class Complex: class Complex:
...@@ -119,9 +117,9 @@ class Complex: ...@@ -119,9 +117,9 @@ class Complex:
raise TypeError, 'Complex numbers are immutable' raise TypeError, 'Complex numbers are immutable'
def __hash__(self): def __hash__(self):
if not self.im: return hash(self.re) if not self.im:
mod = sys.maxint + 1L return hash(self.re)
return int((hash(self.re) + 2L*hash(self.im) + mod) % (2L*mod) - mod) return hash((self.re, self.im))
def __repr__(self): def __repr__(self):
if not self.im: if not self.im:
...@@ -142,8 +140,7 @@ class Complex: ...@@ -142,8 +140,7 @@ class Complex:
return self return self
def __abs__(self): def __abs__(self):
# XXX could be done differently to avoid overflow! return math.hypot(self.re, self.im)
return math.sqrt(self.re*self.re + self.im*self.im)
def __int__(self): def __int__(self):
if self.im: if self.im:
...@@ -238,8 +235,8 @@ def checkop(expr, a, b, value, fuzz = 1e-6): ...@@ -238,8 +235,8 @@ def checkop(expr, a, b, value, fuzz = 1e-6):
except: except:
result = sys.exc_type result = sys.exc_type
print '->', result print '->', result
if (type(result) == type('') or type(value) == type('')): if isinstance(result, str) or isinstance(value, str):
ok = result == value ok = (result == value)
else: else:
ok = abs(result - value) <= fuzz ok = abs(result - value) <= fuzz
if not ok: if not ok:
...@@ -312,13 +309,11 @@ def test(): ...@@ -312,13 +309,11 @@ def test():
(Complex(1), Complex(0,10), 1), (Complex(1), Complex(0,10), 1),
], ],
} }
exprs = testsuite.keys() for expr in sorted(testsuite):
exprs.sort()
for expr in exprs:
print expr + ':' print expr + ':'
t = (expr,) t = (expr,)
for item in testsuite[expr]: for item in testsuite[expr]:
apply(checkop, t+item) checkop(*(t+item))
if __name__ == '__main__': if __name__ == '__main__':
......
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