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.

Truncation warning when using the IQMath-library

Hi,

I am not use if this is the right place to post this topic, but I could not find an application specific or IQMath forum.

I am using the PMSM-application note from TI and the functions use the IQMath library, but whenever I use any of the IQMath functions I get a warning that the number is getting truncated.

see example below: (I simply want to convert 2 to its iq15 representation)

myStructure.somethingiq15 = _IQ15(2);

 

But when I do it like this I get no warnings:

myStructure.somethingiq15 = 2 >> 15;

Any thoughts?

Thanks

  • Alborz,

    can you tell us how you defined myStructure.somethingiq15 ?

    Regards Andreas

  • Hi Alborz,

    TI's IQ library works only and only on 32-bit variables (type long). There is also Q library () which works on 16-bit variables (type int) (http://focus.ti.com/docs/toolsw/folders/print/sprc085.html). This notation seem to be consistent throughout TI documentation (IQ ->32 bit fixed point arithemtic, Q ->16 bit fixed point arithemetic) though I can not offer any guarantees.

     

    Now to your specific problem. _IQ15() macro takes an argument, converts it into floating point number, converts it to IQ number (type long) and returns this value. You are most likely trying to store "long" into an "int", thus compiler generates warning as it should in case of implicit type casts where bits are lost. You can explicitly cast the result of a macro into an int (...thingiq15=(int)_IQ15(2)) in order to acknowledge the cast to the compiler so it will not generate a warning.

    You do not get any warnings in the second case because "2" is an int, as is the result of 15 bit right shift of an int. Thus there is no type cast

     

    Hope this helps

    Regards, Mitja

  • Just a detail, but if you're converting "2" to q15, you should be doing:

    myStructure.somethingiq15 = 2 << 15;

     

  • Thanks for all the replies, I really appreciate it.

    The structure as supplied by TI looks like this.

    /*-----------------------------------------------------------------------------
    Define the structure of the ILEG2DCBUSMEAS Object
    -----------------------------------------------------------------------------*/
    typedef struct { int16 ImeasAGain;     // Parameter: gain for Ia (Q13)
                     int16 ImeasAOffset;   // Parameter: offset for Ia (Q15)
                     int16 ImeasA;         // Output: measured Ia (Q15)
                     int16 ImeasBGain;     // Parameter: gain for Ib (Q13)
                     int16 ImeasBOffset;   // Parameter: offset for Ib (Q15)
                     int16 ImeasB;         // Output: measured Ib (Q15)
                     int16 VdcMeasGain;    // Parameter: gain for Vdc (Q13)
                     int16 VdcMeasOffset;  // Parameter: offset for Vdc (Q15)
                     int16 VdcMeas;        // Output: measured Vdc (Q15)
                     int16 ImeasC;             // Output: computed Ic (Q15)    
                     Uint16 ChSelect;      // Parameter: ADC channel selection
                     void (*init)();       // Pointer to the init function
                     void (*read)();       // Pointer to the read function
                   } ILEG2DCBUSMEAS;

    I would like to set some of the values in this structure. (I have calculated that e.g. ImeasAOffset should be -300 and ImeasAGain should be 0.193359).

    if I create an instance like this:

    ILEG2DCBUSMEAS myInstanceOfILEG2;

    and then try to set the variables like this:

        line1 myInstanceOfILEG2.ImeasAOffset = _IQ15(-300);
        line2 myInstanceOfILEG2.ImeasAGain = _IQ13(0.193359);

    I get this warning: "integer conversion resulted in truncation    line1"
    How should I be doing instead for setting the value to -300? I don´t want to start changing TI library code either.

    Thanks

  • Thanks for the reply.

    Setting the variable as you suggested (like myStructure.ImeasAOffset = (int)_IQ15(-300)) did indeed remove the warning. But what did it actually store?

    (int) _IQ15(-300) converts the integer (-300) to its floating-point representation and then to IQ number (32-bit) and returns the value, and then type casts it to an int (16-bit).

    But how can it type cast a 32-bit to a 16-bit? How much of the actual data was lost?

    Thanks.

  • If we go step by step the result should be

    1. -300 to floating point is -300.0

    2. -300.0 to IQ15 -> -300.0 * 2^15 = -9830400.0

    3. floating -9830400.0 to long -9830400 == 0x00960000

    4. casting long -9830400 to int (taking only lower 16 bits of 32 bit variable - this is according to C standard) you get 0.

    First thing that you must do is ask yourself what kind of numbers can I(you) represent in 16-bit Q15 format (range, precision)? Is it possible to write -300 in Q15 representation?

     

    Regards, Mitja