Your Weird Ruby Item of the Day

UPDATE: See the follow-up here.

Ok. I don't really pretend to know why this is happening, but Chad and I have verified this on several operating systems. And, yes, the number we found was totally by accident.

To get weirded out, just perform the following in irb:

  s = "40.87"
  f = s.to_f
  ft = f * 100
  fti = ft.to_i

If you are like us (Ruby version 1.8.6, patchlevel tested at 0 and 114), your end result there is 4086.

When we started digging a little deeper, the float value stored in the "ft" variable is actually less than 4087, but greater than 4086.9999999999995. We cast around looking for other numbers where this happens, but have been unable to find any.

If anybody has a good explanation, we'd love to hear it.

Oh, and to fix it? Just use:


Of course!

Get In Touch