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.

floating point precision for TMS470R1B1M

Other Parts Discussed in Thread: TMS470R1B1M

Hi,

 

I am using TMS470R1B1M.  I am trying to assign a value of 6.00020002 to a floating point number. 

The assignment is: float a = 6.00020002;

But in debugger the value of a is 6.00019979. 

How do I make the value to show exactly 6.00020002?

Thanks, Jian

 

  • Hello Jian,

    You are seeing the limitation of the 32-bit Single Precision Floating point representation. Not all real numbers can be represented with 100% accuracy. The value you are seeing in the debugger is as close to the desired number of 6.00020002 that can be achieved with this level of floating point precision. This falls back to the principles of floating point and how this translates into real numbers.

    Here is a link to a good online calculator that will perform the conversion and show the closest representation given 32-bit single precision format.

    http://www.binaryconvert.com/result_float.html?decimal=054046048048048050048048048050

    Alternatively, you can do the math yourself using the process described in the wikipedia link below to convert to a binary number and then back its decimal equivalent to see the difference.

    http://en.wikipedia.org/wiki/Single-precision_floating-point_format

     

  • Chuck,

     

    Thanks for the reply.  Is there any way I can make it show 6.00020002 exactly?  This is a software version number.

     

    Thanks, Jian

  • Jian,

    You will not be able to get the exact value in floating point format. I would suggest you using a integer or double precision integer as your software version number.

    Thanks and regads,

    Zhaohong

  • Hello Jian,

    If you are limited to 32 bits, I can't think of any way to do this. However, if you are able to expand the memory allocation for this value, you could use binary encoded decimal.

    For example:

    static struct
    {
        unsigned int integer_val;          /* binary coded decimal value representer integer portion of version */
        unsigned int decimal_val;         /* binary coded decimal value representing decimal portion of version */
    } sw_ver;

     Where assignment is made as follows:

    sw_ver.integer_val = 0x00000006;

    sw_ver.decimal_val = 0x00020002;

    Obviously, this methodology would involve more complex manipulation during software execution for comparing/version checking by software, communication, etc.

    The other option would be to store the data as an ASCII string, but this wouldn't be as easily read and interpreted during debug but would be easier to send over a com channel to a PC.

    Let me know if this helps.

  • Hello Jian,

    Given the length of time that this thread has remained open without any activity, I assume your question has been answered and it will be closed. If possible, please take the time to verify the post that provided the most useful answer in resolving you problem. Thank you for using the E2E forum.