From: "Peña, Botp" Date: 2007-09-04T16:29:34+09:00 Subject: Re: 0.06 == 0.06 returns false in Ruby? 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 # wants to have; e.g. it violates # 'from x==y follows x+z==y+z' > x+z == y+z => true > (x+z).to_s == (y+z).to_s => true > puts (x+z) 0.12E0 => nil # for example # In particular, you don't have # # 'from x==y follows x-y==0' > x==y => true > x-y == 0 => true # for example # # > x, y = 0.6, 6 * 0.1 # > x.to_s == y.to_s # => true # > (x - y).to_s == 0.to_s # => false > x, y = 0.6.to_dec, 6 * 0.1.to_dec > (x - y) == 0 => true > (x - y) == 0.to_dec => true > (x - y).to_s == 0.to_dec.to_s => true kind regards -botp