Take the 2-minute tour ×  "Behavior of “round” function in
Python" observes that Python rounds floats like this:  >>>
round(0.45, 1) >>> round(1.45, 1) >>> round(2.45, 1) >>>
round(3.45, 1) >>> round(4.45, 1) >>> round(5.45, 1) >>>
round(6.45, 1) >>> round(7.45, 1) >>> round(8.45, 1) >>>
round(9.45, 1)  The accepted answer confirms this is caused by
the binary representation of floats being inaccurate, which is
all logical.  Assuming that Ruby floats are just as inaccurate
as Python's, how come Ruby floats round like a human would? Does
Ruby cheat?  1.9.3p194 :009 > 0.upto(9) do |n| 1.9.3p194 :010 >
puts (n+0.45).round(1) 1.9.3p194 :011?>   end share|improve this
question that's strange indeed. printf "%.20f", 1.45 # =>
1.44999999999999995559 1.45.round 1 # => 1.5 –  Karoly Horvath
Mar 31 '13 at 22:59 You mean, why does round(1.45,1) return 1.4
in Python, but 1.45.round(1) return 1.5 in Ruby? –  Colonel
Panic Apr 3 '13 at 23:53 add comment  3 Answers  up vote 8 down
vote accepted   Both implementations ar