diff options
author | Tim Peters <tim.peters@gmail.com> | 2001-03-18 08:21:57 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2001-03-18 08:21:57 (GMT) |
commit | 0f33604e1783d2302006e79713d5d279093405b3 (patch) | |
tree | e98b2d75064317e92782aea742ac35e54fc6dc97 /Lib/test/test_complex.py | |
parent | 7620bbdcbf92eab9c99fc08bbf8e9daaf677e410 (diff) | |
download | cpython-0f33604e1783d2302006e79713d5d279093405b3.zip cpython-0f33604e1783d2302006e79713d5d279093405b3.tar.gz cpython-0f33604e1783d2302006e79713d5d279093405b3.tar.bz2 |
SF bug [ #409448 ] Complex division is braindead
http://sourceforge.net/tracker/?func=detail&aid=409448&group_id=5470&atid=105470
Now less braindead. Also added test_complex.py, which doesn't test much, but
fails without this patch.
Diffstat (limited to 'Lib/test/test_complex.py')
-rw-r--r-- | Lib/test/test_complex.py | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/Lib/test/test_complex.py b/Lib/test/test_complex.py new file mode 100644 index 0000000..ef44fbc --- /dev/null +++ b/Lib/test/test_complex.py @@ -0,0 +1,65 @@ +from test_support import TestFailed +from random import random + +# XXX need many, many more tests here. + +nerrors = 0 + +def check_close_real(x, y, eps=1e-12): + """Return true iff floats x and y "are close\"""" + # put the one with larger magnitude second + if abs(x) > abs(y): + x, y = y, x + if y == 0: + return abs(x) < eps + if x == 0: + return abs(y) < eps + # check that relative difference < eps + return abs((x-y)/y) < eps + +def check_close(x, y, eps=1e-12): + """Return true iff complexes x and y "are close\"""" + return check_close_real(x.real, y.real, eps) and \ + check_close_real(x.imag, y.imag, eps) + +def test_div(x, y): + """Compute complex z=x*y, and check that z/x==y and z/y==x.""" + global nerrors + z = x * y + if x != 0: + q = z / x + if not check_close(q, y): + nerrors += 1 + print `z`, "/", `x`, "==", `q`, "but expected", `y` + if y != 0: + q = z / y + if not check_close(q, x): + nerrors += 1 + print `z`, "/", `y`, "==", `q`, "but expected", `x` + +simple_real = [float(i) for i in range(-5, 6)] +simple_complex = [complex(x, y) for x in simple_real for y in simple_real] +for x in simple_complex: + for y in simple_complex: + test_div(x, y) + +# A naive complex division algorithm (such as in 2.0) is very prone to +# nonsense errors for these (overflows and underflows). +test_div(complex(1e200, 1e200), 1+0j) +test_div(complex(1e-200, 1e-200), 1+0j) + +# Just for fun. +for i in range(100): + test_div(complex(random(), random()), + complex(random(), random())) + +try: + z = 1.0 / (0+0j) +except ZeroDivisionError: + pass +else: + nerrors += 1 + raise TestFailed("Division by complex 0 didn't raise ZeroDivisionError") + +if nerrors: + raise TestFailed("%d tests failed" % nerrors) |