From: Mark Hubbart Date: 2004-05-14T00:52:43+09:00 Subject: Re: Major Addition Bug? On May 12, 2004, at 11:58 PM, Jeff Mitchell wrote: > Just a nitpick: this should be > > class Float > def ==(other) > abs(self - other) < 0.000000000001 > end > end yeah, yeah... :) I realized when I read someone else's version of it that I had forgotten to take the absolute value. :/ Also, I hadn't thought of something else. Since it's a floating point number, you can't have a fixed epsilon for all comparisons. Very large numbers would always fail unless exactly equal, and very small numbers would always pass, even if very different. So, epsilon should be generated from one of the floats you are comparing. That way, if you are comparing floats that are off in the 1e200 or 1e-200 range, you will still get things right. Comparing (num1-num2).abs to num1*1e-8 will check that they are equal within 8 significant digits. class Float def approx_eql?(other, epsilon=1e-8) epsilon *= self # fit epsilon to the number (self - other).abs < epsilon end end This could be extended to allow either an epsilon, or an integer number of digits to check. cheers, --Mark