cancel
Showing results for 
Search instead for 
Did you mean: 

float rounding Cosmic compiler

jfelix
Associate II
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
4 REPLIES 4
jdf25252
Associate II
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
Posted on May 10, 2012 at 16:37

ooops

I meant IEEE754 (3 E's)

jdf

Andrew Neil
Evangelist III
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...
jfelix
Associate II
Posted on June 12, 2012 at 16:34

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