[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

syntax Float equality within epsilon

unbewusst.sein

11/22/2007 8:39:00 AM


i need to test float numbers within an epsilon, then i've extended the
Float clas like that :

class Float
def ===( aFloat, eps = 1.0e-10)
begin
clazz = aFloat.class.to_s
raise "Argument \"#{aFloat}\" must be a Float (being of
#{clazz})." if clazz != "Float"
( self > aFloat - eps ) && ( self < aFloat + eps )
rescue
puts "An error occurred: #{$!}"
nil
end
end
end


this works as expected except when i want not to use the default value
for eps where i couldn't find the correct syntax :
a = 1.000000001
b = 1.00000000012
p ( a ===( b, 0.001) ).to_s
gave me :
[...] syntax error, unexpected ')', expecting tCOLON2 or '[' or '.'
p ( a ===( b, 0.001) ).to_s
^

why ???

--
Une Bévue
3 Answers

Phrogz

11/23/2007 4:17:00 AM

0

On Nov 22, 1:38 am, unbewusst.s...@weltanschauung.com.invalid (Une
Bévue) wrote:
> i need to test float numbers within an epsilon, then i've extended the
> Float clas like that :
>
> class Float
> def ===( aFloat, eps = 1.0e-10)
> begin
> clazz = aFloat.class.to_s
> raise "Argument \"#{aFloat}\" must be a Float (being of
> #{clazz})." if clazz != "Float"
> ( self > aFloat - eps ) && ( self < aFloat + eps )
> rescue
> puts "An error occurred: #{$!}"
> nil
> end
> end
> end
>
> this works as expected except when i want not to use the default value
> for eps where i couldn't find the correct syntax :
> a = 1.000000001
> b = 1.00000000012
> p ( a ===( b, 0.001) ).to_s
> gave me :
> [...] syntax error, unexpected ')', expecting tCOLON2 or '[' or '.'
> p ( a ===( b, 0.001) ).to_s
> ^
>
> why ???

Some operator methods have syntax sugar that prevents them from taking
multiple arguments using simple syntax. You can get around this,
albeit with a slightly less elegant syntax:

irb(main):013:0> class Float
irb(main):014:1> def ===( a, b )
irb(main):015:2> p a, b
irb(main):016:2> end
irb(main):017:1> end
=> nil
irb(main):018:0> a = 5.4
=> 5.4
irb(main):019:0> a.send( :===, 42, 73 )
42
73
=> nil

If this is a case you'd use often, I suggest simply giving it a normal
method name. that you can call without using #send

yermej

11/23/2007 6:07:00 AM

0

On Nov 22, 10:16 pm, Phrogz <phr...@mac.com> wrote:
> On Nov 22, 1:38 am, unbewusst.s...@weltanschauung.com.invalid (Une
>
>
>
> Bévue) wrote:
> > i need to test float numbers within an epsilon, then i've extended the
> > Float clas like that :
>
> > class Float
> > def ===( aFloat, eps = 1.0e-10)
> > begin
> > clazz = aFloat.class.to_s
> > raise "Argument \"#{aFloat}\" must be a Float (being of
> > #{clazz})." if clazz != "Float"
> > ( self > aFloat - eps ) && ( self < aFloat + eps )
> > rescue
> > puts "An error occurred: #{$!}"
> > nil
> > end
> > end
> > end
>
> > this works as expected except when i want not to use the default value
> > for eps where i couldn't find the correct syntax :
> > a = 1.000000001
> > b = 1.00000000012
> > p ( a ===( b, 0.001) ).to_s
> > gave me :
> > [...] syntax error, unexpected ')', expecting tCOLON2 or '[' or '.'
> > p ( a ===( b, 0.001) ).to_s
> > ^
>
> > why ???
>
> Some operator methods have syntax sugar that prevents them from taking
> multiple arguments using simple syntax. You can get around this,
> albeit with a slightly less elegant syntax:
>
> irb(main):013:0> class Float
> irb(main):014:1> def ===( a, b )
> irb(main):015:2> p a, b
> irb(main):016:2> end
> irb(main):017:1> end
> => nil
> irb(main):018:0> a = 5.4
> => 5.4
> irb(main):019:0> a.send( :===, 42, 73 )
> 42
> 73
> => nil
>
> If this is a case you'd use often, I suggest simply giving it a normal
> method name. that you can call without using #send

It can still be called without send. In place of a.send... in the
above example use:

a.===(42, 73)

Still not pretty, but a bit shorter.

Jeremy

unbewusst.sein

11/23/2007 10:25:00 AM

0

yermej <yermej@gmail.com> wrote:

> a.===(42, 73)
>
> Still not pretty, but a bit shorter.

fine, thanks this works great to me !

--
Une Bévue