Problems displaying the float

Bonjour à tous,
Dans le programme de ciblerie interactive, e veux afficher le taux de précision sur l'écran LCD. J'arrive à le faire par moment, mais dès que le nombre de point atteint un certain nombre, le taux affiche un nombre négatif diffèrent du taux réel. Je me suis dit que c’est peut-être à cause du « roll over », mais je suis bloqué.

Voici le code pour le calcul du taux de précision :
TauxPrecision = (float)Point*100/((Zone1 + Zone2 + Zone3 + Zone4 + Zone5 + Zone6) * Cibles1);

Voici les variables utilisées :
float TauxPrecision = 0.0;
int Cibles1 = 600;
int Zone1 = 0;
int Zone2 = 0;
int Zone3 = 0 ;
int Zone4 = 0 ;
int Zone5 = 0 ;
int Zone6 = 0;
unsigned long Point = 0;

Je compte sur vous tous.
Merci d’avance.
Naedio

All of the variables on the right are integers, so the calculation will be an integer calculation. 0 = 1/3 !

(float) is unnecessary and will not be applied until the calculation is complete and not what you want.

Do this instead:

TauxPrecision = Point*100./((Zone1 + Zone2 + Zone3 + Zone4 + Zone5 + Zone6) * Cibles1);

The decimal point in 100. will cause the entire calculation to be handled as floating point.

The decimal point in 100. will cause the entire calculation to be handled as floating point.

You need to be careful about making blanket statements like that. The addition and multiplication may still happen using integer registers. Only the division will happen using floating point registers.

In case of doubt, fall back to something that works.

TauxPrecision = (float) Point * 100.0 / (( (float) Zone1 + (float) Zone2 + (float) Zone3 + (float) Zone4 + (float) Zone5 + (float) Zone6) * (float) Cibles1);

Perhaps using integers is more accurate, but I don't know the maximum values and if the total will fit in a integer.

PaulS is partly correct and I was wrong. Because of the parentheses, the additions and the second multiplication (times Cibles1) will be handled as integer. I believe that the rules of C will cause the first multiplication ( Point*100. ) and the division to be handled as float.

It should not be necessary to put (float) on Zone2, Zone3, Zone4, Zone5, Cibles1, or Point. Putting (float) on Zone1 should be sufficient, I believe (along with the decimal point).

ha ha ha
je reconnais bien ce code.
voici ce u'il faut faire:

  float a = Point/100.0;
  float b = ((Zone1 + Zone2 + Zone3 + Zone4 + Zone5 + Zone6) * Cible1)/100.0;
  TauxPrecision = (a * 100.0)/b;