1 from test_support
import TestFailed
2 from random
import random
4 # XXX need many, many more tests here.
8 def check_close_real(x
, y
, eps
=1e-9):
9 """Return true iff floats x and y "are close\""""
10 # put the one with larger magnitude second
17 # check that relative difference < eps
18 return abs((x-y)/y) < eps
20 def check_close(x, y, eps=1e-9):
21 """Return true iff complexes x
and y
"are close\""""
22 return check_close_real(x
.real
, y
.real
, eps
) and \
23 check_close_real(x
.imag
, y
.imag
, eps
)
26 """Compute complex z=x*y, and check that z/x==y and z/y==x."""
31 if not check_close(q
, y
):
33 print "%r / %r == %r but expected %r" % (z
, x
, q
, y
)
36 if not check_close(q
, x
):
38 print "%r / %r == %r but expected %r" % (z
, y
, q
, x
)
40 simple_real
= [float(i
) for i
in range(-5, 6)]
41 simple_complex
= [complex(x
, y
) for x
in simple_real
for y
in simple_real
]
42 for x
in simple_complex
:
43 for y
in simple_complex
:
46 # A naive complex division algorithm (such as in 2.0) is very prone to
47 # nonsense errors for these (overflows and underflows).
48 test_div(complex(1e200
, 1e200
), 1+0j
)
49 test_div(complex(1e-200, 1e-200), 1+0j
)
53 test_div(complex(random(), random()),
54 complex(random(), random()))
58 except ZeroDivisionError:
62 raise TestFailed("Division by complex 0 didn't raise ZeroDivisionError")
65 raise TestFailed("%d tests failed" % nerrors
)