Article View: fr.comp.lang.c
Article #15411virgule flottante et FLT_EPSILON
From: Tribulations Parallèles
Date: Mon, 26 Sep 2005 21:14
Date: Mon, 26 Sep 2005 21:14
41 lines
2105 bytes
2105 bytes
Bonjour � tous,
Certains m'ont d�j� vu me soucier de FLT_EPSILON (dans float.h) sur d'autres
forums ;-)
En effet, en essayant le petit programme ci-dessous, on se rend compte que
x=15, y=14. Afin de r�ellement obtenir 15, je rajoute FLT_EPSILON au
num�rateur dans y.
J'ai deux questions:
1- x et y me semblent identiques au premier abord. Que se passe-t-il de
diff�rent dans ces deux cas? Pourquoi passer par des noms de variable
semble changer la donne?
2- Quelle d�marche employer pour obtenir la bonne valeur? Rajouter au
num�rateur FLT_EPSILON comme je l'ai fait constitue-t-il une bonne
solution?
Merci d'avance,
Julien
#include <stdio.h>
#include <float.h>
int main(void)
{
float t2=1.5, t1=0.0, h=0.1;
int x=(1.5-0.0)/0.1;
int y=(t2-t1)/h;
int y_corrected=(t2-t1+FLT_EPSILON)/h;
printf("x=%i, y=%i, y_corrected=%i\n",x,y,y_corrected);
return 0;
}
PS:
Je ne me vois pas rajouter 0.5 comme conseill� dans la FAQ (si j'ai bien
compris la signification de cette recommandation), car je pourrais tr�s
bien avoir (t2-t1)/h proche de 1.9, auquel cas c'est bien 1 que je voudrais
obtenir (la partie enti�re), et pas 2.
--
"Allez, Monsieur, allez, et la foi vous viendra." (D'Alembert).
Technical Details
Message-ID:
<43386482$0$26880$626a14ce@news.free.fr>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!infeed-1.proxad.net!news9-e.free.fr!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Thread: f9822,309c5052b3bcc1bf,start X-Google-Attributes: gidf9822,public X-Google-Language: FRENCH,ASCII Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!proxad.net!infeed-1.proxad.net!news9-e.free.fr!not-for-mail Message-ID: <43386482$0$26880$626a14ce@news.free.fr> From: Tribulations Parallèles <Noparatribulations@freeee_._frspaM> Subject: virgule flottante et FLT_EPSILON Newsgroups: fr.comp.lang.c Date: Mon, 26 Sep 2005 23:14:06 +0200 User-Agent: KNode/0.9.1 MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8Bit Lines: 41 Organization: Guest of ProXad - France NNTP-Posting-Date: 26 Sep 2005 23:13:39 MEST NNTP-Posting-Host: 82.228.92.97 X-Trace: 1127769219 news9-e.free.fr 26880 82.228.92.97:35565 X-Complaints-To: abuse@proxad.net