Skip to main content
jfelix
Associate II
May 10, 2012 11:01 AM UTC
Question

float rounding Cosmic compiler

Posted on May 10, 2012 at 13:01

Dear all,

I have some strange roundings with float values: 

for example when I write 0.05 in eeprom (calibration parameters) it becomes 0.049999997 when I read It.

0.2 becomes  0,199999988.

I use Cosmic compiler, I tried to change FLT_DIG in float.h but nothing changes.

If someone has a clue please help me.

Many thanks in advance

#floating-point---just-say-no #stm8-stm8l-float-rounding-cosmic
    This topic has been closed for replies.

    4 replies

    jdf25252
    Associate II
    May 10, 2012 2:36 PM UTC
    Posted on May 10, 2012 at 16:36

    Neither 0.2 nor 0.05 can be represented exactly in floating point format.  Google IEE754 and check out the Wikipedia entries for both the 1985 & 2008 versions of the standard.  Those should give you a good idea of what you're seeing.

    jdf

    jdf25252
    Associate II
    May 10, 2012 2:37 PM UTC
    Posted on May 10, 2012 at 16:37

    ooops

    I meant IEEE754 (3 E's)

    jdf

    Andrew Neil
    Super User
    May 12, 2012 5:58 PM UTC
    Posted on May 12, 2012 at 19:58

    As already noted, this is an inherent artifact of floating-point.

    Another reason to avoid floating-point unless you really, really have a very specific need for it...
    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    jfelix
    jfelixAuthor
    Associate II
    Jun 12, 2012 2:34 PM UTC
    Posted on June 12, 2012 at 16:34

    Many thanks to all, I finally decide not to use float numbers.