2015-02-02 12:52:38 +00:00
|
|
|
# test the functions imported from cmath
|
|
|
|
|
|
|
|
try:
|
|
|
|
from cmath import *
|
|
|
|
except ImportError:
|
|
|
|
print("SKIP")
|
2017-06-08 05:00:57 +01:00
|
|
|
raise SystemExit
|
2015-02-02 12:52:38 +00:00
|
|
|
|
|
|
|
# make sure these constants exist in cmath
|
|
|
|
print("%.5g" % e)
|
|
|
|
print("%.5g" % pi)
|
|
|
|
|
|
|
|
test_values_non_zero = []
|
2020-03-23 02:26:08 +00:00
|
|
|
base_values = (0.0, 0.5, 1.2345, 10.0)
|
2015-02-02 12:52:38 +00:00
|
|
|
for r in base_values:
|
|
|
|
for i in base_values:
|
2020-03-23 02:26:08 +00:00
|
|
|
if r != 0.0 or i != 0.0:
|
2015-02-02 12:52:38 +00:00
|
|
|
test_values_non_zero.append(complex(r, i))
|
2020-03-23 02:26:08 +00:00
|
|
|
if r != 0.0:
|
2015-02-02 12:52:38 +00:00
|
|
|
test_values_non_zero.append(complex(-r, i))
|
2020-03-23 02:26:08 +00:00
|
|
|
if i != 0.0:
|
2015-02-02 12:52:38 +00:00
|
|
|
test_values_non_zero.append(complex(r, -i))
|
2020-03-23 02:26:08 +00:00
|
|
|
if r != 0.0 and i != 0.0:
|
2015-02-02 12:52:38 +00:00
|
|
|
test_values_non_zero.append(complex(-r, -i))
|
2020-08-29 06:14:29 +01:00
|
|
|
test_values = [complex(0.0, 0.0)] + test_values_non_zero
|
2015-02-02 12:52:38 +00:00
|
|
|
print(test_values)
|
|
|
|
|
|
|
|
functions = [
|
2020-03-23 02:26:08 +00:00
|
|
|
("phase", phase, test_values),
|
|
|
|
("polar", polar, test_values),
|
|
|
|
(
|
|
|
|
"rect",
|
|
|
|
rect,
|
|
|
|
((0, 0), (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, 1), (1, -1), (123.0, -456.0)),
|
|
|
|
),
|
|
|
|
("exp", exp, test_values),
|
|
|
|
("log", log, test_values_non_zero),
|
|
|
|
("sqrt", sqrt, test_values),
|
|
|
|
("cos", cos, test_values),
|
|
|
|
("sin", sin, test_values),
|
2015-02-02 12:52:38 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for f_name, f, test_vals in functions:
|
|
|
|
print(f_name)
|
|
|
|
for val in test_vals:
|
|
|
|
if type(val) == tuple:
|
|
|
|
ret = f(*val)
|
|
|
|
else:
|
|
|
|
ret = f(val)
|
|
|
|
if type(ret) == float:
|
|
|
|
print("%.5g" % ret)
|
|
|
|
elif type(ret) == tuple:
|
|
|
|
print("%.5g %.5g" % ret)
|
|
|
|
else:
|
|
|
|
# some test (eg cmath.sqrt(-0.5)) disagree with CPython with tiny real part
|
|
|
|
real = ret.real
|
2018-09-04 08:02:36 +01:00
|
|
|
if abs(real) < 1e-6:
|
2020-03-23 02:26:08 +00:00
|
|
|
real = 0.0
|
2015-02-02 12:52:38 +00:00
|
|
|
print("complex(%.5g, %.5g)" % (real, ret.imag))
|
2020-06-22 01:21:02 +01:00
|
|
|
|
|
|
|
# test invalid type passed to cmath function
|
|
|
|
try:
|
|
|
|
log([])
|
|
|
|
except TypeError:
|
|
|
|
print("TypeError")
|