syntax Float equality within epsilon

U

Une Bévue

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

Phrogz

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
 
Y

yermej

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
^

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top