It seems that I'm missing something fundamental in casting and I don't know what it is!
check this code:
int main(void)
{
float current_i;
float floating_part;
int float_to_int_part;
current_i=13;
printf("----------------- current_i = %f ------------------\n",current_i);
current_i=current_i/10;
printf("new current_i = %f \n",current_i);
floating_part = (current_i-(int)current_i);
printf("floating_part = %f\n",floating_part);
float_to_int_part= (int)(floating_part * 10.0); //i have a problem unserstanding where i went wrong here
printf("float_to_int_part = %d\n",float_to_int_part);
return 0;
}
the output here is
----------------- current_i = 13.000000 ------------------
new current_i = 1.300000
floating_part = 0.300000
float_to_int_part = 2
I expected float_to_int_part
to be 3
but the output is always 2
which is very confusing.
the fact that I don't know why means I'm missing something very basic which could mean I'm missing more basic facts. if someone could tell me what is wrong with my code and suggest a book or a link (that you think will help) to fill this gap.
It's more important for me to know what caused the error. if all my variables are float then the output is correct and my module works.