PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > syntax Float equality within epsilon
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
syntax Float equality within epsilon

Réponse
 
LinkBack Outils de la discussion
Vieux 22/11/2007, 08h38   #1
Une Bévue
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut syntax Float equality within epsilon


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
  Réponse avec citation
Vieux 23/11/2007, 04h16   #2
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: syntax Float equality within epsilon

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
  Réponse avec citation
Vieux 23/11/2007, 06h06   #3
yermej
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: syntax Float equality within epsilon

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
  Réponse avec citation
Vieux 23/11/2007, 10h25   #4
Une Bévue
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: syntax Float equality within epsilon

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
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h19.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,12665 seconds with 12 queries