builtin_float_round.py 522 B

123456789101112131415161718192021222324
  1. # test round() with floats
  2. # check basic cases
  3. tests = [
  4. [0.0], [1.0], [0.1], [-0.1], [123.4], [123.6], [-123.4], [-123.6],
  5. [1.234567, 5], [1.23456, 1], [1.23456, 0], [1234.56, -2]
  6. ]
  7. for t in tests:
  8. print(round(*t))
  9. # check .5 cases
  10. for i in range(11):
  11. print(round((i - 5) / 2))
  12. # test second arg
  13. for i in range(-1, 3):
  14. print(round(1.47, i))
  15. # test inf and nan
  16. for val in (float('inf'), float('nan')):
  17. try:
  18. round(val)
  19. except (ValueError, OverflowError) as e:
  20. print(type(e))