932_dmath_consumers.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. """Fresh integration cases for consumers of dmath, separate from kernel tests."""
  2. import math
  3. import cmath
  4. import operator
  5. from vmath import vec2
  6. def same_float(x, y):
  7. if math.isnan(y):
  8. assert math.isnan(x)
  9. else:
  10. assert x == y, (x, y)
  11. if y == 0.0:
  12. assert math.copysign(1.0, x) == math.copysign(1.0, y)
  13. def test_floating_power_entry_points():
  14. cases = [(5.1875, -4.75), (-5.1875, 9.0), (-5.1875, 0.75),
  15. (-0.0, -9.0), (-0.0, 9.0), (17.0, 8192.0),
  16. (17.0, -8192.0), (1.0, math.nan), (math.nan, 0.0)]
  17. for x, y in cases:
  18. reference = math.pow(x, y)
  19. same_float(x ** y, reference)
  20. same_float(operator.pow(x, y), reference)
  21. def test_integer_power_policy():
  22. # Compute reference modular products using small values / signed endpoints.
  23. cases = [(3, 39, 4052555153018976267), (-3, 39, -4052555153018976267),
  24. (4, 32, 0), (9223372036854775806, 2, 4),
  25. (-9223372036854775807, 2, 1)]
  26. for x, y, expected in cases:
  27. assert x ** y == expected
  28. assert type(x ** y) is int
  29. try:
  30. operator.pow(0, -9)
  31. assert False
  32. except ZeroDivisionError:
  33. pass
  34. def test_floating_division_consumers():
  35. for x, y in [(139.875, 7.5), (-139.875, 7.5), (139.875, -7.5),
  36. (-139.875, -7.5), (3.125e225, 0.25), (-3.125e225, 0.25)]:
  37. quotient, remainder = divmod(x, y)
  38. same_float(x // y, quotient)
  39. same_float(x % y, remainder)
  40. assert quotient == math.floor(quotient)
  41. assert quotient * y + remainder == x
  42. if remainder:
  43. assert math.copysign(1.0, remainder) == math.copysign(1.0, y)
  44. def test_complex_and_rotation_consumers():
  45. for angle in [0.46875, -27.1875, 7.125e17, -2.6875e211]:
  46. sine, cosine = math.sin(angle), math.cos(angle)
  47. z = cmath.rect(1.0, angle)
  48. same_float(z.real, cosine)
  49. same_float(z.imag, sine)
  50. z = cmath.exp(complex(0.0, angle))
  51. same_float(z.real, cosine)
  52. same_float(z.imag, sine)
  53. # Vector storage is float32; this checks the combined sincos consumer.
  54. rotated = vec2(1.0, 0.0).rotate(angle)
  55. assert abs(rotated.x - cosine) <= 6e-8
  56. assert abs(rotated.y - sine) <= 6e-8
  57. same_float(cmath.exp(complex(707.25, 0.0)).real, math.exp(707.25))
  58. test_floating_power_entry_points()
  59. test_integer_power_policy()
  60. test_floating_division_consumers()
  61. test_complex_and_rotation_consumers()