float_divmod.py 522 B

12345678910111213141516171819202122232425
  1. # test floating point floor divide and modulus
  2. # it has some tricky corner cases
  3. def test(x, y):
  4. div, mod = divmod(x, y)
  5. print('%.8f %.8f %.8f %.8f' % (x // y, x % y, div, mod))
  6. print(div == x // y, mod == x % y, abs(div * y + mod - x) < 1e-15)
  7. test(1.23456, 0.7)
  8. test(-1.23456, 0.7)
  9. test(1.23456, -0.7)
  10. test(-1.23456, -0.7)
  11. a = 1.23456
  12. b = 0.7
  13. test(a, b)
  14. test(a, -b)
  15. test(-a, b)
  16. test(-a, -b)
  17. for i in range(25):
  18. x = (i - 12.5) / 6
  19. for j in range(25):
  20. y = (j - 12.5) / 6
  21. test(x, y)