i have a problem where the function somme_diagonale return the right result when called in main, but as soon as i multiply it to ( 2 / 9 ) and print the result it appears to be 0.
function :
{
int i, s = 0 ;
for ( i = 0 ; i < n ; i )
{
s = s mat [ i ] [ i ] ;
}
return s ;
}```
call :
``` printf("resultat est : %f", ( float ) ( 2 / 9 ) * somme_diagonale ( F ) );```
i have tested that somme_diagonale ( F ) returns 165 ( int ).
can someone help me ?
CodePudding user response:
Hi,
in order to have a float
value, either the 2 or the 9 must be cast. This, though, must happen before the division is performed. (2/9)
produces a result of type int
, which only then is cast to float
.
What you should do is replace that piece of code with of these options (sorry in advance, I'm not a fan of spaces):
- (float)2 / 9 * somme_diagonale(F)
- 2 / (float)9 * somme_diagonale(F)
- (float)2 / (float)9 * somme_diagonale(F)