Problème de float

Bonjour à tous,

Je suis fasse à un problème étrange.
Je souhaite faire ce test : valeur1 != valeur2

valeur1 (float) est un champ saisi par l’utilisateur
valeur2 (float) provient de la base de données.

Et dans mon modèle j’ai trois cas de figure :

0.0 != 0.0 --> false
3.79175 != 3.79175 --> false
96.20825 != 96.20825 --> true

Par contre quand je fais ces mêmes cas de figures dans irb, les trois
tests revoient false.

J’avoue je ne comprends pas trop, j’imagine qu’il y a un comportement de
float que je ne connais pas mais je n’arrive pas à pointer le problème.

Merci à ceux qui m’aideront.

Mike


Internet communications are not secure or error free and the contents of
any attachment to this e-mail may contain software viruses or other
defect which might affect your own computer system. RBC Dexia Investor
Services Bank S.A. does not accept liability and/or responsibility for
any damage or loss which may occur or be sustained consequently. The
recipient is responsible for virus checks before opening any attachment.
The information contained in this e-mail may be privileged and/or
confidential. If you are not the intended recipient, you are hereby
notified that any disclosure, use or copying of the information
contained herein is strictly prohibited and may be unlawful. If you
receive this in error, please contact the sender and delete the e-mail.

An electronic message is not binding on its sender.
Any message referring to a binding engagement must be confirmed in
writing and duly signed.

Essaye ça dans irb pour voir…

96.20825 != “96.20825”

ça me renvoie true.

En fait j’ai “solutionné” le problème d’une manière peu orthodoxe…

valeur1.to_s.to_f != valeur2.to_s.to_f

Maintenant le test passe… si quelqu’un à une explication je prend !

Michaël Schwinn
Analyst Developer, IT 2nd Level
IT Transversal Application Management

RBC Dexia Investor Services
T +352 2605 9396
F +352 2605 2008
rbcdexia-is.com http://www.rbcdexia-is.com/

P Please consider your environmental responsibility before printing this
email


From: [email protected] [mailto:[email protected]]
On Behalf Of Michel B.
Sent: jeudi 3 juillet 2008 16:49
To: [email protected]
Subject: [RailsFr] Re: Problème de float

Essaye ça dans irb pour voir…

96.20825 != “96.20825”


Internet communications are not secure or error free and the contents of
any attachment to this e-mail may contain software viruses or other
defect which might affect your own computer system. RBC Dexia Investor
Services Bank S.A. does not accept liability and/or responsibility for
any damage or loss which may occur or be sustained consequently. The
recipient is responsible for virus checks before opening any attachment.

The information contained in this e-mail may be privileged and/or
confidential. If you are not the intended recipient, you are hereby
notified that any disclosure, use or copying of the information
contained herein is strictly prohibited and may be unlawful. If you
receive this in error, please contact the sender and delete the e-mail.


An electronic message is not binding on its sender.

Any message referring to a binding engagement must be confirmed in
writing and duly signed.



Internet communications are not secure or error free and the contents of
any attachment to this e-mail may contain software viruses or other
defect which might affect your own computer system. RBC Dexia Investor
Services Bank S.A. does not accept liability and/or responsibility for
any damage or loss which may occur or be sustained consequently. The
recipient is responsible for virus checks before opening any attachment.
The information contained in this e-mail may be privileged and/or
confidential. If you are not the intended recipient, you are hereby
notified that any disclosure, use or copying of the information
contained herein is strictly prohibited and may be unlawful. If you
receive this in error, please contact the sender and delete the e-mail.

An electronic message is not binding on its sender.
Any message referring to a binding engagement must be confirmed in
writing and duly signed.

Merci, je regarde cela de suite.

Michael


From: [email protected] [mailto:[email protected]]
On Behalf Of Michel B.
Sent: jeudi 3 juillet 2008 17:06
To: [email protected]
Subject: [RailsFr] Re: Problème de float

Pour comprendre pourquoi, un petit coup d’oeil sur les méthodes
suivantes pourrait peut-être t’éclairer :

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_float.html#Float.to_s
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_float.html#Float._lt_eq_lt
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.to_f
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String._eq_eq

Indice : c’est une histoire de type…


Internet communications are not secure or error free and the contents of
any attachment to this e-mail may contain software viruses or other
defect which might affect your own computer system. RBC Dexia Investor
Services Bank S.A. does not accept liability and/or responsibility for
any damage or loss which may occur or be sustained consequently. The
recipient is responsible for virus checks before opening any attachment.

The information contained in this e-mail may be privileged and/or
confidential. If you are not the intended recipient, you are hereby
notified that any disclosure, use or copying of the information
contained herein is strictly prohibited and may be unlawful. If you
receive this in error, please contact the sender and delete the e-mail.


An electronic message is not binding on its sender.

Any message referring to a binding engagement must be confirmed in
writing and duly signed.



Internet communications are not secure or error free and the contents of
any attachment to this e-mail may contain software viruses or other
defect which might affect your own computer system. RBC Dexia Investor
Services Bank S.A. does not accept liability and/or responsibility for
any damage or loss which may occur or be sustained consequently. The
recipient is responsible for virus checks before opening any attachment.
The information contained in this e-mail may be privileged and/or
confidential. If you are not the intended recipient, you are hereby
notified that any disclosure, use or copying of the information
contained herein is strictly prohibited and may be unlawful. If you
receive this in error, please contact the sender and delete the e-mail.

An electronic message is not binding on its sender.
Any message referring to a binding engagement must be confirmed in
writing and duly signed.

Hello Mike,

peut-être utilises-tu le type float pour ta valeur en base de données
(MySQL j’imagine ?) MySQL stocke les “float” sur 4 octets (cf
http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html) ce qui ne
permet pas de mémoriser un décimal avec une grande précision.

Dans ton cas, si Price est un model avec :price_as_float une colonne
en float, tu auras:

Price.create(:price_as_float => 96.20825).reload.price_as_float
=> 96.2083
(et non 96.20825)

Côté base tu auras un:

INSERT INTO prices (price_as_float) VALUES(96.20825)
qui stockera au final 96.2083 et non 96.20825

Si tu dois stocker des chiffres avec une meilleure précision, tu peux
t’orienter vers t.decimal :price_as_decimal, :precision => 30, :scale
=> 20 (précision et scale à adapter en fonction de tes besoins -
regarde la doc MySQL de près). Ex:

Price.create(:price_as_decimal => 96.20825).reload.price_as_decimal == 96.20825
=> true

Si tu veux tout savoir sur le pourquoi du comment (stockage en mémoire
des flottants), je pense que tu trouveras l’article suivant
intéressant:

http://www.codeproject.com/KB/dotnet/ExtremeFloatingPoint1.aspx

good luck!

Thibaut Barrère / LoGeek

http://blog.logeek.fr - learning content for developers
http://evolvingworker.com - tools for a better day
http://faire-autrement.fr - la simplicité au quotidien

Salut Thibaut!

Il est vraiment que j’aurai dû le mentionner dès le début, c’est une erreur de
ma part et je m’en excuse.
Je travaille avec Oracle et non MySql. Par contre j’utilise bien le
float comme type. Il est cependant probable que le comportement soit
identique, je vais m’y pencher.

En tout cas merci pour le lien sur les “extreme floating”, ça m’a l’air
très intéressant. Je me le garde dans un coin car la je n’ai pas trop le
temps d’y jeter un oeil :wink:

Encore merci!

ps: si tu le vois, passe le bonjour à ton frère ,-)

Mike

Pour comprendre pourquoi, un petit coup d’oeil sur les méthodes
suivantes
pourrait peut-être t’éclairer :

http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_float.html#Float.to_s
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_float.html#Float._lt_eq_lt
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String.to_f
http://www.ruby-doc.org/docs/ProgrammingRuby/html/ref_c_string.html#String._eq_eq

Indice : c’est une histoire de type…

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs