From: Michael Ulm Date: 2007-09-04T20:10:34+09:00 Subject: Re: 0.06 == 0.06 returns false in Ruby? Pe�a wrote: > From: Michael Ulm [mailto:michael.ulm@isis-papyrus.com] > # Daniel Berger wrote: > # > 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 > > w a little help fr bigdecimal, a little goes a long long way > >> class Float >> def to_dec >> BigDecimal.new(self.to_s) >> end >> end > => nil >> class Fixnum >> def to_dec >> BigDecimal.new(self.to_s) >> end >> end > => nil > >> x=0.06.to_dec >> y=0.05.to_dec + 0.01.to_dec >> x==y > => true >> z=0.02.to_dec+0.04.to_dec >> y==z > => true >> z==y > => true >> x==z > => true >> z==x > => true --snip-- However (among other problems): > x = (1.0/3.0).to_dec > y = 1.0.to_dec / 3.0.to_dec > x == y # => false HTH, Michael