float_divmod_relaxed.py 705 B

123456789101112131415161718192021222324252627282930313233
  1. # test floating point floor divide and modulus
  2. # it has some tricky corner cases
  3. # pyboard has 32-bit floating point and gives different (but still
  4. # correct) answers for certain combinations of divmod arguments.
  5. def test(x, y):
  6. div, mod = divmod(x, y)
  7. print(div == x // y, mod == x % y, abs(div * y + mod - x) < 1e-6)
  8. test(1.23456, 0.7)
  9. test(-1.23456, 0.7)
  10. test(1.23456, -0.7)
  11. test(-1.23456, -0.7)
  12. a = 1.23456
  13. b = 0.7
  14. test(a, b)
  15. test(a, -b)
  16. test(-a, b)
  17. test(-a, -b)
  18. for i in range(25):
  19. x = (i - 12.5) / 6
  20. for j in range(25):
  21. y = (j - 12.5) / 6
  22. test(x, y)
  23. # test division by zero error
  24. try:
  25. divmod(1.0, 0)
  26. except ZeroDivisionError:
  27. print('ZeroDivisionError')