From: quixoticsycophant@... (Jeff Mitchell) Date: 2004-05-13T15:58:51+09:00 Subject: Re: Major Addition Bug? Mark Hubbart wrote in message news:<528A9534-A443-11D8-ADAC-000502FDD5CC@mac.com>... > On May 12, 2004, at 10:27 AM, Sean O'Dell wrote: > > rather than rounding, you might consider: > > (625.91 + 900.00 + 22.00) - 1547.91 > 0.0000000001 > ==>false > > or even, for the scope of your app: > > class Float > def ==(other) > self - other < 0.000000000001 > end > end > ==>nil > (625.91 + 900.00 + 22.00) == 1547.91 > ==>true > > or, if you aren't into redefing #==, you might just make a method, say, > #approx_eql?(other) for Float. > Just a nitpick: this should be class Float def ==(other) abs(self - other) < 0.000000000001 end end Coming from a background in mathematics, I have personal affection for: class Float def within?(epsilon, other) abs(self - other) < epsilon end end EPSILON = 1e-8 # ... if a.within?(EPSILON, b) # ... I read this as, "if a is within epsilon of b" -- a common phrase in analysis.