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

And:

k = (-140.14 * 100)

=> -14014.0k.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

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??).

Thanks to anyone who can test this out.

Kind Regards,

Samuel W.