From: Robert Klemme Date: 2005-03-31T23:35:04+09:00 Subject: Re: Integer/Float oddity schrieb im Newsbeitrag news:1112277873.331258.267880@f14g2000cwb.googlegroups.com... > not really an oddity, more a lack of understanding on my part regarding > ruby's internal type conversion... > > (100*(1.15-1)) > => 15.0 > > (100*(1.15-1)).to_int > => 14 > > (100*(1.15-1)).to_int.round > => 15 > > 15.0.to_int > => 15 > > what am i missing here? i never would have thought to use .round to > get the 'correct' result unless someone told me. by correct i mean if > i was doing this with a paper and pencil, i would expect to get 15, not > 14. > > any insight would be appreciated, especially a way (if possible) to > override this behaviour so i dont run into this in the future. > > thanks, > horati0 This is a typical limitation of numeric calculations. The reason for what you see is that 1.15-1 is smaller than 0.15: >> (1.15-1) - 0.15 => -8.32667268468867e-17 consequently >> (100 * (1.15-1))-15 => -8.88178419700125e-15 this is negative => (100 * (1.15-1)) is smaller than 15 and thus it's correctly converted to 14 by to_int. Kind regards robert