t422.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # a list of the numbers that Skulpt has trouble rounding correctly; all others should be true
  2. bugs = [-0.5,-0.025,-0.055,0.045,-0.0025,-0.0035,0.0045,0.0055,-250,-350,-450,-550]
  3. def helper(iterable,expect,n=None):
  4. if n:
  5. for i in iterable:
  6. r = round(i,n)
  7. if abs(r-expect) > (1/10.0**(n+1)) and i not in bugs:
  8. print False,i," expected: ",expect," result: ",r,abs(r-expect)
  9. else:
  10. for i in iterable:
  11. r = round(i)
  12. if abs(r-expect) > 0.000001 and i not in bugs:
  13. print False,i," expected: ",expect," result: ",r,abs(r-expect)
  14. print "\n-1.4 to 1.4, no ndigit"
  15. helper([x/10.0 for x in range(-5,-15,-1)],-1)
  16. helper([x/10.0 for x in range(4,-5,-1)],0)
  17. helper([x/10.0 for x in range(5,15)],1)
  18. print "\n-1.49 to 1.49, no ndigit"
  19. helper([x/100.0 for x in range(-50,-150,-1)],-1)
  20. helper([x/100.0 for x in range(40,-50,-1)],0)
  21. helper([x/100.0 for x in range(50,150)],1)
  22. print "\n-0.064 to -0.025, ndigit=2"
  23. helper([x/1000.0 for x in range(-25,-35,-1)],-0.03,2)
  24. helper([x/1000.0 for x in range(-35,-46,-1)],-0.04,2)
  25. helper([x/1000.0 for x in range(-46,-55,-1)],-0.05,2)
  26. helper([x/1000.0 for x in range(-55,-65,-1)],-0.06,2)
  27. print "\n0.025 to 0.064, ndigit=2"
  28. helper([x/1000.0 for x in range(25,35)],0.03,2)
  29. helper([x/1000.0 for x in range(35,46)],0.04,2)
  30. helper([x/1000.0 for x in range(46,55)],0.05,2)
  31. helper([x/1000.0 for x in range(55,65)],0.06,2)
  32. print "\n-0.0064 to -0.0025, ndigit=3"
  33. helper([x/10000.0 for x in range(-25,-35,-1)],-0.003,3)
  34. helper([x/10000.0 for x in range(-35,-46,-1)],-0.004,3)
  35. helper([x/10000.0 for x in range(-46,-56,-1)],-0.005,3)
  36. helper([x/10000.0 for x in range(-56,-65,-1)],-0.006,3)
  37. print "\n0.0025 to 0.0064, ndigit=3"
  38. helper([x/10000.0 for x in range(25,35)],0.003,3)
  39. helper([x/10000.0 for x in range(35,46)],0.004,3)
  40. helper([x/10000.0 for x in range(46,56)],0.005,3)
  41. helper([x/10000.0 for x in range(56,65)],0.006,3)
  42. print "\n-649 to -250, ndigit=-2"
  43. helper(range(-250,-350,-1),-300,-2)
  44. helper(range(-350,-450,-1),-400,-2)
  45. helper(range(-450,-550,-1),-500,-2)
  46. helper(range(-550,-650,-1),-600,-2)
  47. print "\n250 to 649, ndigit=-2"
  48. helper(range(250,350),300,-2)
  49. helper(range(350,450),400,-2)
  50. helper(range(450,550),500,-2)
  51. helper(range(550,650),600,-2)