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.

CCS/MSP430FR5994: Floating point to Fixed point conversion logic

Part Number: MSP430FR5994


Tool/software: Code Composer Studio

Hello,

I have tried to copy some floating-point data to a fixed-point array (like q15 or iq31 type array) and vice versa. I used memcpy function for that and they work fine. But I am interested in the math of the conversion. 

For example:

Float                                                Fixed (iq31)

0.101607598                                   1037047717
1.40129846e-45 (DEN)                   1.0
0.00472378731                                1000000000


From the above conversion result, I did not find any pattern that describes the math. Besides, what does the (DEN) mean?

  • memcpy() will not do any conversion, it will just copy the bits.

  • I suggest using the _Q15() macro from dsplib/include/DSPLib_Support.h. The comment says there's also one in IQMathlib somewhere, if that's what you're using.

    [Edit: Fixed path name]

  • I know memcpy just copy the bits.

    Keith Barkley said:

    memcpy() will not do any conversion, it will just copy the bits.

  • I know how does _Q15() represent integer data and vice versa. But my question is about the fixed-point representation of the floating-point numbers.

  • Just what is your question?

    A fixed point number *is* an integer with an implied decimal point.

    Q15 means that the lower 15 bits represent the fractional part of the number. That number is simply stored/typed as an integer. To see how it is done, use Bruce's suggestion and look at the conversion code.

  • Let me give you an example.

    Lets say, I have a floating point number 1.0
    The binary representation of that number in 32 bit is 00111111100000000000000000000000
    now, If I copy this number to an iq31 variable, then it's integer representation is 1065353216
    And it's fixed point representation is  (1065353216/ (2^31)) = 0.496

    That means a floating point number 1.0 is represented as 0.496 in iq31.
    Same way 0.1 is represented as 0.483 in iq31

    So my question is, is there any math so that I can directly maps between them skipping binary representation.

    This is important for me because, I want to copy some data from floating to fixed point variable and do some math. Then copy again the data to a floating point number. But it seems, after the math, the result is inaccurate. As a result, I need to know the conversion (or representation) math so that I can retirive the accurate result. I hope i am able to explain you well. 

  • > That means a floating point number 1.0 is represented as 0.496 in iq31.

    You were fine -- in the sense of making true statements -- until you got here.

    1) You can't represent 1.0 in iq31. (You can represent -1.0, though.)

    2) Picking a valid example, the value -1.0 isn't represented as  -1.496 (0xbf800000) in iq31, it's represented as -1 (0x80000000).

    All the previous paragraph says is "0x3f800000 in iq31 represents 0.496". Which is true but not very useful.

    As Keith has said, you can't change from float to fixed by copying bits. If you try to do this, your results will be wrong.

    E.g. changing from float to Q15 is multiplying by 2^15 (32768), then rounding, then converting to an "int". The _Q15() macro does this for you.

    [Edit: small clarification]

  • Just for the record, here is the macro:

    #define _IQ15(A)                ((_iq15)((A) * ((_iq15)1 << 15)))

**Attention** This is a public forum