Tim Pease
7/7/2006 4:02:00 PM
Bug in my own method :(
def =~( other )
epsilon_eql?(coerce(other)[0], EPSILON)
end
<sigh> it's definitely a Friday
TwP
On 7/7/06, Tim Pease <tim.pease@gmail.com> wrote:
> > On 7/7/06, Martin DeMello <martindemello@gmail.com> wrote:
> >
> > This is ugly, because Float#== is a two-argument method that you're
> > faking with one argument and one global constant. An array argument
> > might be a nice piece of syntactic sugar, though I'd still prefer to
> > overload =~ instead, e.g. a =~ [b, epsilon] with a =~ b defaulting to
> > Float::EPSILON.
> >
> > Also, note that your method definition needs to be
> >
> > def =~ (o); ((o - self)/o).abs <= EPSILON; end
> >
> > you want relative, not absolute, error margins.
> >
> > martin
> >
>
> Martin, I like the syntax. To make it more like Float#==
>
> class Float
> def =~( other )
> epsilon_eql?(coerce(other), EPSILON)
> end
>
> def epsilon_eql?( other, epsilon )
> return false unless other.instance_of? self.class
> ((other-self)/other).abs <= epsilon
> end
> end
>
> I think this gives the best of both worlds. A simple =~ syntax for
> using the default epsilon, and the slightly less elegant epsilon_eql?
> syntax when you want to sepcify your own.
>
> Blessings,
> TwP
>
>