From: Robert Klemme Date: 2007-08-31T18:41:14+09:00 Subject: Re: 0.06 == 0.06 returns false in Ruby? 2007/8/31, Daniel DeLorme : > 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? And who decides about the size of epsilon and which algorithm to choose? There is no one size fits all answer to that hence leaving Float#== the way it is (i.e. compare for exact identical values) is the only viable option. Otherwise you would soon see similar questions on the list, i.e., "how come it sometimes works and sometimes it doesn't". And they become more difficult to answer as the cases are likely less easy to spot / explain. Kind regards robert