cmath_fun.py 1.6 KB

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