From: "Wolfgang NĂ¡dasi-Donner" Date: 2007-01-20T05:20:49+09:00 Subject: Re: Negative Float conversion to Fixnum Drew Raines schrieb: > I'm sure this has been hashed out somewhere on the list before, but > my searches have turned up fruitless. I had a strange issue come up > while diagnosing a unit test failure. > > Notice the first dialog compared with the rest of this IRB session. > What causes -3930.0 to be converted to a Fixnum as -3929 only after > it's been multiplied by 100 or 100.0? Even weirder is that it seems > to only be *this* number. Can I workaround this? > > >> (-39.30 * 100).to_i > => -3929 > >> (-39.30 * 100.0).to_i > => -3929 > >> -3930.0.to_i > => -3930 > >> (-39.31 * 100).to_i > => -3931 > >> (-9999.99 * 100).to_i > => -999999 > $ ruby -v > ruby 1.8.5 (2006-12-04 patchlevel 2) [i686-linux] > > Thanks. > > -Drew irb(main):001:0> 3930-39.30*100 => 4.54747350886464e-013 Rounding errors are typical for floating point numbers. In this case the result of -39.30*100 ist a little bit smaller than 3930, so to_i works correct.