# Syntax Float equality within epsilon

**URL:** <https://www.ruby-forum.com/t/syntax-float-equality-within-epsilon/120969>\
**Category:** Ruby\
**Created:** [November 22, 2007, 9:41am UTC](https://www.ruby-forum.com/t/syntax-float-equality-within-epsilon/120969 "2007-11-22T09:41:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [November 22, 2007, 9:41am UTC](https://www.ruby-forum.com/t/syntax-float-equality-within-epsilon/120969/1 "2007-11-22T09:41:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [November 23, 2007, 5:22am UTC](https://www.ruby-forum.com/t/syntax-float-equality-within-epsilon/120969/2 "2007-11-23T05:22:04Z")

</div>

On Nov 22, 1:38 am, removed\_email\_address@domain.invalid (Une  
Bévue) wrote:

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

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [November 23, 2007, 5:40am UTC](https://www.ruby-forum.com/t/syntax-float-equality-within-epsilon/120969/3 "2007-11-23T05:40:10Z")

</div>

Quoth Phrogz:

> > #{clazz})." if clazz != “Float”  
> > a = 1.000000001  
> > multiple arguments using simple syntax. You can get around this,  
> > 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

Or you could use “normal” method call notation:

irb(main):009:0\> a.===(32, 73)  
32  
73  
=\> nil

Regards,

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [November 23, 2007, 7:10am UTC](https://www.ruby-forum.com/t/syntax-float-equality-within-epsilon/120969/4 "2007-11-23T07:10:44Z")

</div>

On Nov 22, 10:16 pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > ```
> > begin
> > 
> > ```
> 
> > why ???  
> > =\> 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

---

<div class="post-metadata">

**Author:** ![Une\_BSSSSvS\_ue](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/2acd7d/32.png) [@Une\_BSSSSvS\_ue](https://www.ruby-forum.com/u/Une_BSSSSvS_ue)\
**Post date:** [November 23, 2007, 11:31am UTC](https://www.ruby-forum.com/t/syntax-float-equality-within-epsilon/120969/5 "2007-11-23T11:31:57Z")

</div>

yermej [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

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

fine, thanks this works great to me !
