On Sep 3, 5:32 pm, Stefan Rusterholz <apei... / gmx.net> wrote: > Matthias WçÄhter wrote: > > Stefan Rusterholz schrieb: > >> Matthias WçÄhter wrote: > >>> What I mean is that any float stored as a IEEE 754 double (64 bit), > >>> like 0.1 with hex notation 0x3FB999999999999A should have a distinct > >>> string representation such that (a==b) == (a.to_s==b.to_s). Note > >>> that the next higher double 0.1+2.0**-56 with hex notation > >>> 0x3FB999999999999B has the same .to_s representation, which is not good. > >> I disagree. As long as you're calculating, you are working with floats > >> anyway, so you have maximum precision. Comparing floats should be done > >> using delta comparison anyway too. > > > Which, as discussed in the 0.06 thread, is hard to accomplish > > nevertheless. Anyway. > > Is it? That's news to me. > class Numeric > def delta?(other, delta=Float::EPSILON*16) # chose the delta wisely > (self-other).abs < delta > end > alias =~ delta? > end > > (0.01 + 0.05).in_delta?(0.06) # => true > 0.01 + 0.05 =~ 0.06 # => true # can't set the delta, is similarly evil > as == class Numeric def in_delta?(other, delta=Float::EPSILON*16) # chose the delta wisely (self-other).abs < delta end alias =~ in_delta? end p 1.23456789e4.in_delta?( 1.234567892e4 ) p 1.23456789e-1.in_delta?( 1.234567892e-1 ) p 1.23456789e-6.in_delta?( 1.234567892e-6 ) p 1.23456789e-11.in_delta?( 1.234567892e-11 ) ==== output ==== false false true true