mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
Patch #1177597: Correct various bugs, add comments.
This commit is contained in:
parent
629496b77c
commit
4a1e48c566
1 changed files with 36 additions and 9 deletions
|
@ -62,8 +62,8 @@
|
||||||
# Complex for +,-,cmp
|
# Complex for +,-,cmp
|
||||||
# Polar for *,/,pow
|
# Polar for *,/,pow
|
||||||
|
|
||||||
|
|
||||||
import types, math
|
import types, math
|
||||||
|
import sys
|
||||||
|
|
||||||
twopi = math.pi*2.0
|
twopi = math.pi*2.0
|
||||||
halfpi = math.pi/2.0
|
halfpi = math.pi/2.0
|
||||||
|
@ -98,14 +98,22 @@ def Im(obj):
|
||||||
class Complex:
|
class Complex:
|
||||||
|
|
||||||
def __init__(self, re=0, im=0):
|
def __init__(self, re=0, im=0):
|
||||||
|
_re = 0
|
||||||
|
_im = 0
|
||||||
if IsComplex(re):
|
if IsComplex(re):
|
||||||
im = i + Complex(0, re.im)
|
_re = re.re
|
||||||
re = re.re
|
_im = re.im
|
||||||
|
else:
|
||||||
|
_re = re
|
||||||
if IsComplex(im):
|
if IsComplex(im):
|
||||||
re = re - im.im
|
_re = _re - im.im
|
||||||
im = im.re
|
_im = _im + im.re
|
||||||
self.__dict__['re'] = re
|
else:
|
||||||
self.__dict__['im'] = im
|
_im = _im + im
|
||||||
|
# this class is immutable, so setting self.re directly is
|
||||||
|
# not possible.
|
||||||
|
self.__dict__['re'] = _re
|
||||||
|
self.__dict__['im'] = _im
|
||||||
|
|
||||||
def __setattr__(self, name, value):
|
def __setattr__(self, name, value):
|
||||||
raise TypeError, 'Complex numbers are immutable'
|
raise TypeError, 'Complex numbers are immutable'
|
||||||
|
@ -224,7 +232,6 @@ def exp(z):
|
||||||
|
|
||||||
|
|
||||||
def checkop(expr, a, b, value, fuzz = 1e-6):
|
def checkop(expr, a, b, value, fuzz = 1e-6):
|
||||||
import sys
|
|
||||||
print ' ', a, 'and', b,
|
print ' ', a, 'and', b,
|
||||||
try:
|
try:
|
||||||
result = eval(expr)
|
result = eval(expr)
|
||||||
|
@ -238,8 +245,28 @@ def checkop(expr, a, b, value, fuzz = 1e-6):
|
||||||
if not ok:
|
if not ok:
|
||||||
print '!!\t!!\t!! should be', value, 'diff', abs(result - value)
|
print '!!\t!!\t!! should be', value, 'diff', abs(result - value)
|
||||||
|
|
||||||
|
|
||||||
def test():
|
def test():
|
||||||
|
print 'test constructors'
|
||||||
|
constructor_test = (
|
||||||
|
# "expect" is an array [re,im] "got" the Complex.
|
||||||
|
( (0,0), Complex() ),
|
||||||
|
( (0,0), Complex() ),
|
||||||
|
( (1,0), Complex(1) ),
|
||||||
|
( (0,1), Complex(0,1) ),
|
||||||
|
( (1,2), Complex(Complex(1,2)) ),
|
||||||
|
( (1,3), Complex(Complex(1,2),1) ),
|
||||||
|
( (0,0), Complex(0,Complex(0,0)) ),
|
||||||
|
( (3,4), Complex(3,Complex(4)) ),
|
||||||
|
( (-1,3), Complex(1,Complex(3,2)) ),
|
||||||
|
( (-7,6), Complex(Complex(1,2),Complex(4,8)) ) )
|
||||||
|
cnt = [0,0]
|
||||||
|
for t in constructor_test:
|
||||||
|
cnt[0] += 1
|
||||||
|
if ((t[0][0]!=t[1].re)or(t[0][1]!=t[1].im)):
|
||||||
|
print " expected", t[0], "got", t[1]
|
||||||
|
cnt[1] += 1
|
||||||
|
print " ", cnt[1], "of", cnt[0], "tests failed"
|
||||||
|
# test operators
|
||||||
testsuite = {
|
testsuite = {
|
||||||
'a+b': [
|
'a+b': [
|
||||||
(1, 10, 11),
|
(1, 10, 11),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue