-------- Original-Nachricht --------
> Datum: Tue, 28 Oct 2008 04:31:06 +0900
> Von: "guille lists" <guilledist / gmail.com>
> An: ruby-talk / ruby-lang.org
> Betreff: Re: float equality

> Thanks a lot for the answers and references, and sorry for not having
> check deeper on google
> (http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/9655).
> 
> So I guess that if one wants to work for example with float numbers in
> the range [0,1], the best way to do it is by normalising from an
> integer interval depending on the precision you want, say [0,100] for
> two decimal digits precision, and so on. Is there any other better
> approach? Does the use of BigDecimal impose a severe penalty on
> performance?
> 
> 
> guille

Dear guille,

you could use some approximate equality check:

class Float
   def approx_equal?(other,threshold)
       if (self-other).abs<threshold    # "<" not exact either ;-)
          return true
       else
          return false
       end
   end
end

a=0.1
b=1.0-0.9
threshold=10**(-5)

p a.approx_equal?(b,threshold)


Best regards,

Axel 


-- 
Ist Ihr Browser Vista-kompatibel? Jetzt die neuesten 
Browser-Versionen downloaden: http://www.gmx.net/de/go/browser