From: Michael Ulm Date: 2007-09-04T15:56:12+09:00 Subject: Re: 0.06 == 0.06 returns false in Ruby? Daniel Berger wrote: > > On Aug 31, 12:25 am, Michael Ulm wrote: > > > >> Unfortunately, there is no easy solution to this problem. > > > > Here's my naive, DWIM approach: > > class Float > def ==(other) > self.to_s == other.to_s > end > end > Oh yes, nice one. This fulfills 'from x==y and y==z follows x==z' Unfortunately it does not fulfill some other properties one wants to have; e.g. it violates 'from x==y follows x+z==y+z' for example > x, y = 1.0/3.0, 0.333333333333333 > x.to_s == y.to_s # => true > (x+x).to_s == (y+x).to_s # => false In particular, you don't have 'from x==y follows x-y==0' for example > x, y = 0.6, 6 * 0.1 > x.to_s == y.to_s # => true > (x - y).to_s == 0.to_s # => false HTH, Michael