Float

nicolas66 Messages postés 116 Date d'inscription mercredi 25 décembre 2002 Statut Membre Dernière intervention 4 janvier 2009 - 29 janv. 2004 à 19:31
nicolas66 Messages postés 116 Date d'inscription mercredi 25 décembre 2002 Statut Membre Dernière intervention 4 janvier 2009 - 30 janv. 2004 à 19:42
Salut tout le monde,
j'ai un souci majeur avec une fonction que j'ai faite. Elle doit retourner un float et elle le retourne bien car j'ai affiché cette valeur. Le problème c'est que quand ds le "main" je déclare une variable float et que je l'initialise avec ma fonction, j'obtient un nombre totalement différent ! Voici mon code, j'espère que certains sauront d'où ca peut venir :

#include
#include
#include <math.h>
#define EPSILON 0.04

using namespace std;

float f2(float x)
{
return exp(x)*(x*x-3*x+1);
}

float dicho(float a,float b)
{
float c = (a+b)/2;
float f2a = f2(a);
float f2c = f2(c);

if(f2a*f2c == 0 || abs(b-a)< EPSILON)
return c; // valeur renvoyée correcte
else if(f2a*f2c > 0)
dicho(c,b);
else
dicho(a,c);
}

main()
{
float d = dicho(0,1);
cout << d;
// valeur affichée incorrecte
}

merci d'avance ..

:big) Neo [[mailto:benouse.gerbouli@caramail.com email]] [site web]

3 réponses

lastpixl Messages postés 56 Date d'inscription samedi 28 juin 2003 Statut Membre Dernière intervention 16 février 2006
29 janv. 2004 à 20:49
J'ai modifié un peu ton programme et il a l'air de fonctionner...

Par contre maintenant la fonction dicho ne renvoie plus rien, et on va chercher c dans main, qui est maintenant une variable globale.

Il n'y avait pas le prototype de la fonction : ca ne gene pas BCC5.5, mais visualC++ si.
Je ne sais pas pourquoi ca ne marchait pas... Sur mon ordi ton code se plante, j'ai une exception Vérification de pile en virgule flottante 0xc0000092.

#include
#include
#include <math.h>
#define EPSILON 0.04

using namespace std;
float c=0;

float f2(float x)
{
return exp(x)*(x*x-3*x+1);
}
void dicho(float a,float b)
{
c = (a+b)/2;
float f2a = f2(a);
float f2c = f2(c);

if(f2a*f2c == 0 || abs(b-a)< EPSILON)
{
return;
//return c; // valeur renvoyée correcte}
}
else {if(f2a*f2c > 0)
{
dicho(c,b);}
else
{
dicho(a,c);}
}

}

main()
{
dicho(0,1);
cout << c;
cin >> c;
// valeur affichée incorrecte
}

Lastpix'l
0
cs_djl Messages postés 3011 Date d'inscription jeudi 26 septembre 2002 Statut Membre Dernière intervention 27 novembre 2004 7
29 janv. 2004 à 20:53
vire using namespace std;
int main()
{
}

utilise fabs (valeur absolue? ) pour type float

et comment tu ve renvoyer une valeur si t'appelle dicho recursivement sans return?

#include 
#include <math.h>
#define EPSILON 0.04f

float f2(float x)
{
return (float)exp(x)*(x*x-3*x+1);
}

float dicho(float a,float b)
{
float c = (a+b)/2;
float f2a = f2(a);
float f2c = f2(c);

if(f2a*f2c == 0.0f || fabs(b-a)< EPSILON)
return c; // valeur renvoyée correcte
if(f2a*f2c > 0)
return dicho(c,b);

return dicho(a,c);
}

int main()
{ 
float d = dicho(0.0f,1.0f);
cout <<d <<endl;
} 
0
nicolas66 Messages postés 116 Date d'inscription mercredi 25 décembre 2002 Statut Membre Dernière intervention 4 janvier 2009
30 janv. 2004 à 19:42
Exact t'as raison, merci :)

:big) Neo [[mailto:benouse.gerbouli@caramail.com email]] [site web]
0
Rejoignez-nous