float rounding Cosmic compiler
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2012-05-10 4:01 AM
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 discussion is locked. Please start a new topic to ask your question.
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2012-05-10 7:36 AM
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.
jdfOptions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2012-05-10 7:37 AM
Posted on May 10, 2012 at 16:37
ooops
I meant IEEE754 (3 E's) jdfOptions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2012-05-12 10:58 AM
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.
A complex system designed from scratch never works and cannot be patched up to make it work.
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2012-06-12 7:34 AM
Posted on June 12, 2012 at 16:34Many thanks to all, I finally decide not to use float numbers.
