This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Assigning an Integer variable to a float variable yields wrong result



void SomeFunct(...)

{

uint32_t intVal;

float floatVal;

/* some code that sets intVal (to 32 for example) */ 

floatVal = intVal;

}

When I run this code the floatVal does not = 32.0 but rather 4.484155e-44 (0x00000020 when decoded as a floating point number). 

The dis-assembly for this c code is:

1089 floatRpm = rpm;
$C$L137:
00003a42:     9800               LDR R0, [SP]
00003a44:     EE000A10     FMSR S0, R0
00003a48:     EEB80A40     FUITOS S0, S0


What do I need to do to get the expected floating point val?  Here's the basic info about the compiler setup.

  • Hi Curt,

       Try this below(in bold). See "Type Casting in C".

       uint32_t intVal;

       float floatVal;

       floatVal = (float)intVal;

    -kel

  • This could be a result of the debugger giving confusing information when optimisation is used. E.g. when the Optimisation level was set to zero, which allows some intermediate results to be be stored in registers, I was able to repeat the symptoms:

     

    Note that due to optimisation the Variables view shows both the integer intRpm and floating point floatRpm variables in register R0, with the floating point shown with the apparently incorrect value of 4.484155e-44. After stepping out of the test_int_to_float function the value of floatRpm in the caller was correctly shown as 32.0 (in the stack of the caller).

    If the test program is re-compiled with the Optimisation level set to Disable then the debugger shows the expected value of the variables, which are now on the stack rather than in registers:

    The optimisation level is set by the CCS project properties Build -> ARM Compiler -> Optimization -> Optimization Level (--opt_level, -O)

  • Markel,

    I probably should have mentioned I had tried that as well.  

    Thanks for the thought,

    Curt

  • Thanks, debuging with -O0  was the problem.