2002-07-30 20:27:12 -03:00
|
|
|
from test.test_support import TestFailed, vereq
|
2001-03-18 04:21:57 -04:00
|
|
|
from random import random
|
|
|
|
|
2001-12-13 15:57:53 -04:00
|
|
|
# These tests ensure that complex math does the right thing; tests of
|
|
|
|
# the complex() function/constructor are in test_b1.py.
|
|
|
|
|
2001-03-18 04:21:57 -04:00
|
|
|
# XXX need many, many more tests here.
|
|
|
|
|
|
|
|
nerrors = 0
|
|
|
|
|
2001-09-06 20:00:21 -03:00
|
|
|
def check_close_real(x, y, eps=1e-9):
|
|
|
|
"""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-9):
|
2001-03-18 04:21:57 -04:00
|
|
|
"""Return true iff complexes x and y "are close\""""
|
2001-09-06 20:00:21 -03:00
|
|
|
return check_close_real(x.real, y.real, eps) and \
|
|
|
|
check_close_real(x.imag, y.imag, eps)
|
2001-03-18 04:21:57 -04:00
|
|
|
|
|
|
|
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
|
2001-05-29 19:18:09 -03:00
|
|
|
print "%r / %r == %r but expected %r" % (z, x, q, y)
|
2001-03-18 04:21:57 -04:00
|
|
|
if y != 0:
|
|
|
|
q = z / y
|
|
|
|
if not check_close(q, x):
|
|
|
|
nerrors += 1
|
2001-05-29 19:18:09 -03:00
|
|
|
print "%r / %r == %r but expected %r" % (z, y, q, x)
|
2001-03-18 04:21:57 -04:00
|
|
|
|
|
|
|
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()))
|
|
|
|
|
2001-12-28 21:02:21 -04:00
|
|
|
for i in range(100):
|
|
|
|
if not complex(random() + 1e-6, random() + 1e-6):
|
|
|
|
raise TestFailed("complex(random(), random()) should be true")
|
|
|
|
|
|
|
|
if complex(0.0, 0.0):
|
|
|
|
raise TestFailed("complex(0.0, 0.0) should be false")
|
|
|
|
|
2001-12-29 13:34:57 -04:00
|
|
|
vereq(complex(5.3, 9.8).conjugate(), 5.3-9.8j)
|
2001-12-29 10:31:46 -04:00
|
|
|
|
2001-12-28 21:02:21 -04:00
|
|
|
try:
|
|
|
|
print int(5+3j)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TestFailed("int(complex()) didn't raise TypeError")
|
|
|
|
|
|
|
|
try:
|
|
|
|
print float(5+3j)
|
|
|
|
except TypeError:
|
|
|
|
pass
|
|
|
|
else:
|
|
|
|
raise TestFailed("float(complex()) didn't raise TypeError")
|
|
|
|
|
2001-03-18 04:21:57 -04:00
|
|
|
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)
|