From: "Matthias Wächter" Date: 2007-09-03T19:09:18+09:00 Subject: Re: 0.06 == 0.06 returns false in Ruby? On 31.08.2007 16:07, Davor wrote: > Hi, > Try converting to strings e.g: > irb(main):004:0> (0.05 + 0.01).to_s == 0.06.to_s > => true I think what puzzles is while 0.05+0.01 == 0.06 returns false, the strings _do_ match. def float_check(a,b) p [a==b,a.to_s == b.to_s] end float_check(0.06,0.06) # -> [true,true] float_check(0.05+0.01,0.06) # -> [false,true] Most other programming languages either have no format-less .to_s function (like in C) or return values at inspection time so that people get aware of the issue more easily. In Python it is obvious that 0.05+0.01 == 0.06 is false. $ python >>> 0.05 0.050000000000000003 >>> 0.01 0.01 >>> 0.05+0.01 0.060000000000000005 >>> 0.06 0.059999999999999998 I don't understand why Ruby 'rounds' values with .inspect (and .to_s) opposed to Marshal.dump. Compare its output with the one Python gives (this code is for Ruby 1.8.6): [0.05,0.01,0.05+0.01,0.06].each do |v| p Marshal.dump(v).sub(/....([^\000]*).*/,'\1') end Output: "0.050000000000000003" "0.01" "0.060000000000000005" "0.059999999999999998" Did I miss something here, is there a way to make Float#to_s (and, thus, Float#inspect) give the same output as Marshal? - Matthias