Re: 0.06 == 0.06 returns false in Ruby?



There is also a class called BigDecimal (or something like that) if
you want to have really accurate numbers and no floating-point errors.

On 8/30/07, Dan Zwell <dzwell@xxxxxxxxx> wrote:
Dan Zwell wrote:
To compare floats, you must ask whether they are within a certain
threshold of each other.
Epsilon = 0.00000000001
return (num1-num2).abs < Epsilon # num1 == num2

Dan



Oops, I didn't realize that Ruby comes with an Epsilon. This should be a
fine test for equality:

return (num1-num2).abs < Float::EPSILON

Now, whether Float#==(other) should make this check might be worth
thinking about, but I really have no opinion on the matter--I'm used to
not comparing floats like this.

Dan



.



Relevant Pages

  • Re: Floating point arithmetic (epsilon)
    ... > my trouble lies in finding a reasonable accuracy threshold to stop the ... > SINGLE-FLOAT ... But subtracting delta ... > misunderstanding how to interpret these epsilon constants. ...
    (comp.lang.lisp)
  • Re: Float comparisons: how to do them properly
    ... whether a value is greater then a threshold ... Adding an epsilon value does not change this problem, ... you compare fabswith epsilon. ... of whether I use compiler X or Y. ...
    (comp.lang.c)
  • Re: Float comparisons: how to do them properly
    ... whether a value is greater then a threshold ... you compare fabswith epsilon. ... It can as well be b + epsilon. ... In other words, you want IDENTICAL garbage, rather than different-smelling ...
    (comp.lang.c)