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.

IQMath to Float compile error

Hi,

I'm getting a compile error with the following line of code when changing from IQMath to floating point (done in switch in IQMathLib.h):

 clarke1.As=_IQ15toIQ((AdcRegs.ADCRESULT0>>1)-_IQ15(0.55))<<1;

 "2xPM_Motors.c", line 656: error: expression must have integral type

 

Since I'm doing floating point, I could rewrite the code to simple floating point, but I'm curious as to why this no longer compiles? (It appears the last left shift is an issue.)

Thanks.

  • Tim,

    A shift operation moves bits left or right.  Its an easy way to scale an integer by 2, 4, or divide by 1/2, 1/4 etc. 

    For an integer this makes sense because of the format of the number.  Floating-point numbers, however,  have quite a different format.  It has a sign bit, an exponent field and a mantissa.  Each of these fields is a fixed number of bits and the corresponding normal range of values is given by an equation like this: (-1)signbits ×2exponentbits-127× 1.Mantissa.  If you were to simply shift bits one way or the other it would not give you the results you expect.

    In your project you could do something like this - it would still allow you to quickly flip between float and IQ

    //
    // Example:
    // Convert from float to IQ15
     
    //
    // If MATH_TYPE == IQ_MATH
    // Use the IQmath conversion function
    //
     
    #if MATH_TYPE == IQ_MATH
    PwmReg = (int16)_IQtoIQ15(Var1);
     
    //
    // If MATH_TYPE == FLOAT_MATH
    // Scale by 2^15 = 32768.0
    //
     
    #else // MATH_TYPE is FLOAT_MATH
    PwmReg = (int16)(32768.0L*Var1);
    #endif

     

    Hope that helps

    -Lori

  • Hi Lori,

    I had somehow forgot that I was now working in floating point, and so that IS an invalid operation.

    Thanks for the reminder.

    Tim