|
| 1 | +# test the functions imported from cmath |
| 2 | + |
| 3 | +try: |
| 4 | + from cmath import * |
| 5 | +except ImportError: |
| 6 | + print("SKIP") |
| 7 | + import sys |
| 8 | + sys.exit() |
| 9 | + |
| 10 | +# make sure these constants exist in cmath |
| 11 | +print("%.5g" % e) |
| 12 | +print("%.5g" % pi) |
| 13 | + |
| 14 | +test_values_non_zero = [] |
| 15 | +base_values = (0.0, 0.5, 1.23456, 10.) |
| 16 | +for r in base_values: |
| 17 | + for i in base_values: |
| 18 | + if r != 0. or i != 0.: |
| 19 | + test_values_non_zero.append(complex(r, i)) |
| 20 | + if r != 0.: |
| 21 | + test_values_non_zero.append(complex(-r, i)) |
| 22 | + if i != 0.: |
| 23 | + test_values_non_zero.append(complex(r, -i)) |
| 24 | + if r != 0. and i != 0.: |
| 25 | + test_values_non_zero.append(complex(-r, -i)) |
| 26 | +test_values = [complex(0., 0.),] + test_values_non_zero |
| 27 | +print(test_values) |
| 28 | + |
| 29 | +functions = [ |
| 30 | + ('phase', phase, test_values), |
| 31 | + ('polar', polar, test_values), |
| 32 | + ('rect', rect, ((0, 0), (0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (-1, 1), (1, -1), (123., -456.))), |
| 33 | + ('exp', exp, test_values), |
| 34 | + ('log', log, test_values_non_zero), |
| 35 | + ('log10', log10, test_values_non_zero), |
| 36 | + ('sqrt', sqrt, test_values), |
| 37 | + ('cos', cos, test_values), |
| 38 | + ('sin', sin, test_values), |
| 39 | +] |
| 40 | + |
| 41 | +for f_name, f, test_vals in functions: |
| 42 | + print(f_name) |
| 43 | + for val in test_vals: |
| 44 | + if type(val) == tuple: |
| 45 | + ret = f(*val) |
| 46 | + else: |
| 47 | + ret = f(val) |
| 48 | + if type(ret) == float: |
| 49 | + print("%.5g" % ret) |
| 50 | + elif type(ret) == tuple: |
| 51 | + print("%.5g %.5g" % ret) |
| 52 | + else: |
| 53 | + # some test (eg cmath.sqrt(-0.5)) disagree with CPython with tiny real part |
| 54 | + real = ret.real |
| 55 | + if abs(real) < 1e15: |
| 56 | + real = 0. |
| 57 | + print("complex(%.5g, %.5g)" % (real, ret.imag)) |
0 commit comments