space.ship.traveller / gmail.com wrote:
> Hi,
> 
> I've come across a strange bug in ruby (running 1.8.6 on Linux, but
> confirmed also in 1.8.5 on an older Mac).
> 
>>> (-140.14 * 100).to_i
> => -14013
> 
> The desired result is, obviously, -14014. Strangely enough:
> 
>>> (-140.14 * 100)
> => -14014.0
> 
This might explain it:

irb(main):007:0> sprintf("%0.12f" % (-140.14 * 100))
=> "-14013.999999999998"

> And:
> 
>>> k = (-140.14 * 100)
> => -14014.0
>>> k.to_i
> => -14013
> 
> HOWEVER......
> 
>>> -14014.0.to_i
> => -14014
> 
> Is this a strange behaviour or what?
> 
> Workaround is like this:
> 
>>> k.to_s.to_i
> => -14014
Or:

irb(main):014:0> (-140.14 * 100).round
=> -14014

> 
> Can someone please confirm this strange behaviour?... I know it is
> also happening for some other numbers too:
> 
> (0..1000).each do |n|
> 	n = n.to_f + (n.to_f / 100)
> 	k = (n * 100)
> 	if k != k.to_s.to_f
> 	  puts "K does not equal itself!? #{k} != #{k.to_s.to_f}"
>   end
> 
> 	if k.to_i != k.to_s.to_i
> 		puts "Error with k = #{k}!? #{k.to_i} != #{k.to_s.to_i}"
> 	end
> end
> 
> I know a lot about floating point numbers, but this is really bizarre
> behaviour.
> 
> Expected behaviour would be for no errors in the above test example. I
> don't expect floating point to be accurate (this is obvious), but I do
> expect floating point to be consistent (whole number floating point is
> guaranteed to be accurate with IEEE floating point standard right??).
The product of two whole numbers isn't, though.  Apparently.  It's been 
a while since I knew lots about floating point representation, but 
that's what this test case is telling me.

-- 
Alex