math_fun.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. # Tests the functions imported from math
  2. try:
  3. from math import *
  4. except ImportError:
  5. print("SKIP")
  6. raise SystemExit
  7. test_values = [-100., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 100.]
  8. test_values_small = [-10., -1.23456, -1, -0.5, 0.0, 0.5, 1.23456, 10.] # so we don't overflow 32-bit precision
  9. unit_range_test_values = [-1., -0.75, -0.5, -0.25, 0., 0.25, 0.5, 0.75, 1.]
  10. functions = [('sqrt', sqrt, test_values),
  11. ('exp', exp, test_values_small),
  12. ('log', log, test_values),
  13. ('cos', cos, test_values),
  14. ('sin', sin, test_values),
  15. ('tan', tan, test_values),
  16. ('acos', acos, unit_range_test_values),
  17. ('asin', asin, unit_range_test_values),
  18. ('atan', atan, test_values),
  19. ('ceil', ceil, test_values),
  20. ('fabs', fabs, test_values),
  21. ('floor', floor, test_values),
  22. ('trunc', trunc, test_values),
  23. ('radians', radians, test_values),
  24. ('degrees', degrees, test_values),
  25. ]
  26. for function_name, function, test_vals in functions:
  27. print(function_name)
  28. for value in test_vals:
  29. try:
  30. print("{:.5g}".format(function(value)))
  31. except ValueError as e:
  32. print(str(e))
  33. tuple_functions = [('frexp', frexp, test_values),
  34. ('modf', modf, test_values),
  35. ]
  36. for function_name, function, test_vals in tuple_functions:
  37. print(function_name)
  38. for value in test_vals:
  39. x, y = function(value)
  40. print("{:.5g} {:.5g}".format(x, y))
  41. binary_functions = [('copysign', copysign, [(23., 42.), (-23., 42.), (23., -42.),
  42. (-23., -42.), (1., 0.0), (1., -0.0)]),
  43. ('pow', pow, ((1., 0.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
  44. ('atan2', atan2, ((1., 0.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
  45. ('fmod', fmod, ((1., 1.), (0., 1.), (2., 0.5), (-3., 5.), (-3., -4.),)),
  46. ('ldexp', ldexp, ((1., 0), (0., 1), (2., 2), (3., -2), (-3., -4),)),
  47. ('log', log, ((2., 2.), (3., 2.), (4., 5.), (0., 1.), (1., 0.), (-1., 1.), (1., -1.), (2., 1.))),
  48. ]
  49. for function_name, function, test_vals in binary_functions:
  50. print(function_name)
  51. for value1, value2 in test_vals:
  52. try:
  53. print("{:.5g}".format(function(value1, value2)))
  54. except (ValueError, ZeroDivisionError) as e:
  55. print(type(e))