From: Lee Griffiths Date: 2008-08-05T22:16:55+09:00 Subject: Re: Whose fault is my problem? Di Mo wrote: > On Mon, Aug 4, 2008 at 7:38 AM, The Podman wrote: > >> * Float == Float does not perform any delta difference checking. In a >> way, this is expected. In another way, it's not. > > If you don't like the == method of Float just crack it open and change > it: > > a = 12.60000000000001 > b = 12.59999999999999 > > puts a > puts b > > puts a == b # => false > > class Float > def ==(other) > fuzz = 1.0E-10 > max = [self.abs, other.abs].max > ( self - other ).abs / max < fuzz > end > end > > puts a == b # => true > > I would only recommend doing this for your tests though, other code > might want the original == method intact. > > > -- > > -Daniel I did just that for this project. :) Well, not exactly that, but something similiar (I copied and pasted it from an old C file I had). I still have the old == aliased as "old_equ". Infact my new equ method uses it as an inital check. I'm used to dealing with floats. I _know_ the ways in which they should be compared. However, right now I don't require my points to be deadly accurate. I'll be happy just as long as they're floating. I also don't see Ruby being used for precise mathmatical simulations (even though that's what I'm using it for. Erk!) and think it should have some built in support for delta-comparison. After all, if it's happy enough to display something as "2.85", why can't it just treat it like 2.85?* Sometimes that 0.3e-27 really, really isn't important and should just /go away/ and stop ruining things :) My main issue that sparked the first post was that so many things will just assume == gives the correct result. It does give the correct result, but my problem is when they're imbedded deep inside two objects that are compared to each other using ==, and these objects just naivey ask all their internal attributes if they == other.attribute . So why doesn't Ruby have, by default: a) a built in =~ or Float#approx(other) method? b) The ability to "switch on" and "off" wether == or =~ is used whenever == is called (I hope that makes sense!)? Whilst for now my extensions to Float will do (for me, at least), I think that the default Ruby float class should deal with this issue without having to drag up BigDecimal. What do you all think? Is built in fuzzy checking, and the option to use it on or off to much to ask? * Of course, to many people the 0.3e-27 difference is very important, but those people could turn off the fuzzy checking :) -- Posted via http://www.ruby-forum.com/.