Compiller corrupts Floating-point types.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Compiller corrupts Floating-point types.

757 Views
vasyapupking
Contributor I

I use CodeWarrior IDE version 5.9.0 and MC9S08AW60. I found that it incorrectly handles floating point variables. For the values up to 65535.9999 everything is ok. Values from 65536.0000 becomes 0.0004 greather.  I see 65536.0004 instead 65536.0000. Values starting from 524288.0000 becomes 0.0049 greater. Values starting from 8388608.0000 add 0.0499 and so on. Note, that 65536 = 2^16, 524288 = 2^19, 8388608=2^23. Is there any solution for this problem?

void test_double(void)

{

double val1;

double val2;

U8 i;

char buf[20];

    clr_str(0);

    i=0;

    val1 = 65535.9999;

    sprintf (buf,"%5.4f", val1);

    while (buf[i])

         out_lcd (buf[i++]);

   

    clr_str(1);  

    i=0;

    val2 = 65536.0000;

    sprintf (buf,"%5.4f", val2);

    while (buf[i])

         out_lcd (buf[i++]);

    for(;;);

}

126838_126838.jpgIMG_20160112_085847.jpg

Labels (1)
0 Kudos
2 Replies

516 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Vasya,

For floats we always use IEEE32. For double IEEE64 is also available. Note that when creating a project from the wizard you get three choices: no floating point, IEEE32 for both float and double and IEEE32 for float and IEEE64 for double. Also note that for the values you mention, the loss of precision occurs even when encoding this numbers. For instance, take 82.78062. This number is represented as 0x42a58fad, which actually means 82.780617. This is not wrong but is actually the closest value we can get using IEEE32.

Well any floating related computation may cause precision loss as floating variable itself uses IEEE32 to code. This is the disadvantage and also the characteristic for MCU processing floating data.

If you just want to display floating value in string, may I suggest you use sprintf to insert a decimal point inside an integer value:

d_vr1=91000;

sprintf(str,"%ld.%ld",(long)d_vr1/(long)10000,(long)d_vr1%10000); 

After executing above code, the output in “str” is “9.1000”

does this answer your question?


Have a great day,
Jennie Zhang

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

516 Views
vasyapupking
Contributor I

Hi Jennie,

Thanks for your reply. This solution is not suitable for me. My project contains

number of high precision values in the range 0.00001 to 99999999999.9999. I need

to perform various mathemathics on them, store and output to LCD and not only. So

I must handle them as floating point types. I can not treat them as integer +

remainder. Also, it all works on AVR and IAR. The same code. So I think, it might

be some library bug, related to floating point types storage.

Sincerely.

Dersu Uzalah.

13.01.2016, 7:33:33 пользователь ZhangJennie (admin@community.freescale.com)

написал:

NXP Community

=============

Compiller corrupts Floating-point types.

0 Kudos