From: Jamis Buck Date: 2004-10-28T05:04:43+09:00 Subject: Re: Rounding error, (100.0 * 9.95).to_i == 994 Jason DiCioccio wrote: > Hello! > > --On Thursday, October 28, 2004 03:48:56 +0900 Jamis Buck > wrote: > >> Bill Atkins wrote: >> >>> OK, that would make sense if v's value was 994.5, but it seems to be >>> 995.0. Why should to_i round down like that? >> >> >> But it isn't 995.0. It is 994.99999999883 or something. IRB displays it >> as 995.0, rounding it. >> >> - Jamis > > > I'm confused now.. 100.0 * 9.95 is clearly 995. So what exactly is the > issue with floating point numbers is making this come out to > 994.999999999983 (or wahtever ;))? If every language is plagued by this > problem, then I'd be curious to know the history behind it.. http://mathworld.wolfram.com/RoundoffError.html It's a problem caused by the fact that floating point numbers cannot be represented exactly, and when you perform operations on those inexact numbers, the different between the approximation and the true becomes greater and greater. - Jamis -- Jamis Buck jgb3@email.byu.edu http://www.jamisbuck.org/jamis