From: Dan Zwell Date: 2007-08-31T17:18:31+09:00 Subject: Re: 0.06 == 0.06 returns false in Ruby? Daniel DeLorme wrote: > Michael Ulm wrote: >>> this floating problem is a faq and is very surprising on such a >>> very high language such as ruby. can we address this? maybe create >>> flag like $EPSILON=0 or something, or may flag to revert to rational >>> or bigdeci like $FLOAT_PROCESSOR=RATIONAL... >> >> Unfortunately, there is no easy solution to this problem. Here is a >> catalog of often proposed solutions and why they do not work: >> >> 2 Always use (x-y).abs < (x.abs + y.abs) * Float::EPSILON) as a test >> for equality. >> >> Better than the first proposal, but won't work if the rounding error >> gets too large after a complex computation. >> In addition, (1) and (2) suffer from the problem that x==y and y==z do >> not imply x==z. > > But it would fix 99% of problems. It would be worth it just for the sake > of reducing those questions on the list :-P > > Seriously though, since floating point calculations are approximative to > start with, what would be wrong with making them more intuitive > approximations? IMHO Float should use the above algorithm for == and > reserve normal floating point arithmetic for eql? > > Daniel > > Michael wrote very convincingly that there is no simple solution that will work in all cases. I'm convinced, at least. If there is no solution that works 100% of the time, we can't give the illusion that there is. To do so would be to teach bad programming practices to newcomers, and that's not fair. The current "==" in okay because it works the way a moderately experienced programmer would expect. A perfect "==" that could deal with floats would be even better, but we aren't gonna get that. A "==" that seems like magic and almost always works is really pretty dangerous. Dan