This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

floating point

Hi every body

I don't know why I don't have the float
part of the temp var.


float temp;
int H_Value;
int L_Value;
int value = 55;

temp = value / 10;
H_Value = (int)temp;
L_Value = (int)(temp - H_value)*10;


the value int temp is 5 and not 5.5

Thanks Kobi

  • The program behaves exactly right --- it's your expectation that's incorrect here.

    The bug in your program is that there's not
    a single manifest floating point number in it.

  • Or, to be more precise:

    temp = value / 10;

    is an integer (value) divided by an integer literal ("10"), which results in a integer (5). That integer is then assigned to temp, and converted to a float representation (5.0) in the process.

    You want to do the division in floating point. So, try something like:

    temp = (float)value / 10;

    or

    temp = value / 10.0;

    to have the expression on the right hand side evaluate as a float.

    It occurs to me that you actually want the division to truncate for H_value. You could leave out "temp" entirely:

    H_value = value / 10;
    L_value = (value % 10) * 10;

    This code has no floating point at all, which will do nice things for speed and space requirements.

    (I'm not entirely sure you really want that last "* 10". If you're trying to separate the number into multiples of ten, and the leftover part, then it should just be

    L_value = value % 10,

    and your original code should have been

    L_value = temp - H_value * 10;

    )

    Hmmm: Jon asked a few weeks back how often people actually needed both quotient and remainder from a single divison. Here's yet another case.

  • And note that this is all standard 'C' - nothing specifically to do with Keil.

    Therefore, in this case, reading the Keil Manual will not help you - because the Keil manual assumes that you already know ANSI 'C', so only goes into the implementation-specific details.

    You will need to refer to K&R, or another good 'C' textbook.