math_domain.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Tests domain errors in math functions
  2. try:
  3. import math
  4. except ImportError:
  5. print("SKIP")
  6. raise SystemExit
  7. inf = float('inf')
  8. nan = float('nan')
  9. # single argument functions
  10. for name, f, args in (
  11. ('fabs', math.fabs, ()),
  12. ('ceil', math.ceil, ()),
  13. ('floor', math.floor, ()),
  14. ('trunc', math.trunc, ()),
  15. ('sqrt', math.sqrt, (-1, 0)),
  16. ('exp', math.exp, ()),
  17. ('sin', math.sin, ()),
  18. ('cos', math.cos, ()),
  19. ('tan', math.tan, ()),
  20. ('asin', math.asin, (-1.1, 1, 1.1)),
  21. ('acos', math.acos, (-1.1, 1, 1.1)),
  22. ('atan', math.atan, ()),
  23. ('ldexp', lambda x: math.ldexp(x, 0), ()),
  24. ('radians', math.radians, ()),
  25. ('degrees', math.degrees, ()),
  26. ):
  27. for x in args + (inf, nan):
  28. try:
  29. ans = f(x)
  30. print('%.4f' % ans)
  31. except ValueError:
  32. print(name, 'ValueError')
  33. except OverflowError:
  34. print(name, 'OverflowError')
  35. # double argument functions
  36. for name, f, args in (
  37. ('pow', math.pow, ((0, 2), (-1, 2), (0, -1), (-1, 2.3))),
  38. ('fmod', math.fmod, ((1.2, inf), (1.2, 0), (inf, 1.2))),
  39. ('atan2', math.atan2, ((0, 0),)),
  40. ('copysign', math.copysign, ()),
  41. ):
  42. for x in args + ((0, inf), (inf, 0), (inf, inf), (inf, nan), (nan, inf), (nan, nan)):
  43. try:
  44. ans = f(*x)
  45. print('%.4f' % ans)
  46. except ValueError:
  47. print(name, 'ValueError')