jemnotesversion 2 / featuring this entry or see all/search

Dec 15
It seems Ruby rounds inconsistently—when told to display to 1 or 2 significant figures, it rounds 5s to 1 and 2, respectively—instead of being the same. Not sure why!
>> [1.14, 1.15, 1.16].map{|x| "%.1f" % x}
=> ["1.1", "1.1", "1.2"]
>> [1.4, 1.5, 1.6].map{|x| "%.0f" % x}
=> ["1", "2", "2"]
Edit: Nope, not a bug. This is because 1.15 gets translated to an internal, imprecise float which is slightly less than 1.15. I thought I understood most of the implications of floating-point imprecision—apparently not.