[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: Float#==. Legacy?

Tim Pease

7/7/2006 3:58:00 PM

> 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

5 Answers

Tim Pease

7/7/2006 4:02:00 PM

0

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
>
>

Ara.T.Howard

7/7/2006 4:09:00 PM

0

Tim Pease

7/7/2006 6:29:00 PM

0

Done.

http://rcrchive.net/rc...

On 7/7/06, ara.t.howard@noaa.gov <ara.t.howard@noaa.gov> wrote:
> On Sat, 8 Jul 2006, Tim Pease wrote:
>
> > 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
>
> hi guys-
>
> i've written this at least 10 times - how bout an rcr?
>
> -a
> --
> suffering increases your inner strength. also, the wishing for suffering
> makes the suffering disappear.
> - h.h. the 14th dali lama
>
>

Joel VanderWerf

7/7/2006 7:31:00 PM

0


The Float#=~ that Martin suggested is nice, but it still doesn't help
with one of the problems which (I think) the OP wanted:

x = 1.2
y = 1.2000000000000001
p(x =~ y) # => true
p([x] =~ [y]) # => false

Array#=~ is not defined now, and Object#=~ always returns false (see
rb_obj_pattern_match()).

Should Array#=~ attempt to propagate the #=~ call through its members?
Or is there a good reason why this is left unspecified by ruby.

The same question arises for hashes...

Any ideas?

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

Guillaume Marcais

7/8/2006 2:23:00 AM

0

Le 7 juil. 06, à 15:30, Joel VanderWerf a écrit :

>
> The Float#=~ that Martin suggested is nice, but it still doesn't help
> with one of the problems which (I think) the OP wanted:
>
> x = 1.2
> y = 1.2000000000000001
> p(x =~ y) # => true
> p([x] =~ [y]) # => false
>

Yes, this is what I was referring to.

I came about a little differently. Mainly that calling
Float#within_delta or equivalent is easy when I have the float itself,
but if is embedded in an Array or Hash, it renders #== useless on the
structure:

[1+1, 3] == [2, 3] # => true
[0.1+0.2, 3] == [0.3, 3] # => false

For the second case, one would needs to loop on the elements of the
array.

And my second realization is if Float#== is (almost) always a mistake,
why not change it to something useful most of the time. Changing
Float#== in my code the way I did make me a little nervous as it is
changing a pretty fundamental behavior, but on the other hand it is
hardly ever used and it made sense in my case.

Guillaume.