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.

Incorrect conversion from float to long

The following gives the incorrect answer for lfp which should be 0x004E47AE but i get a trucated 0x000047AE. If I change the declaration of "long lfp" to "volatile long lfp" it gives me the correct answer. Clue: with the original declaration lfp is assigned to a register, with the volatile it get stored in memory.

    float z, f, fp;
    int eb;
    long lfp;

    z = log(setpoint) / log(2);
    eb = (int)z + 0x7F;
    f = setpoint / 16;
    fp = (f-1) * 8388608; // 2 << 23
    lfp = (long)(fp);

Is this a bug?

I am using CCS v5.1.0.09000, MSP430 Compiler Tools v4.0.0, optimization is set to 0

 

Thank you, Don