There are three cardinal rules that I find helpful in using floating
point. Nearly all the documents that I see relating to it are
elaborations of these three -- so I'll just give you the rules, and let
you extrapolate from there:
Rule 1: Floating point is a resource hog. This isn't an issue if
you're using a PC or other 'big iron' machine, because floating point is
already built in and hogging resources even when you're not using them.
But if you're designing a small system with a fixed-point processor,
or a system built around an FPGA, it counts in either low speed, large
silicon area, or some combination thereof.
Rule 2: Floating point cannot be counted on to be exact. Never, ever
put something in your code that boils down to
if (this_float == that_float)
Do this, and you will get lots and lots of false negatives. Most of the
time that I do this, I find that I can get away with
if (this_float < that_float)
-- and then I need to be strict about doing just one test. The rest of
the time you have to use
if (fabs(this_float - that_float) < some_tolerance)
-- and choosing the tolerance can be a bear.
(Note that this is what you seem to have run afoul of).
Rule 3: Floating point isn't as precise as you think. If you have a 32
bit floating point number, then you really only have 25 effective bits
of precision in the mantissa. Nearly always you can fix this by just
using double-precision (which Scilab and I think Matlab do as routine)
-- but on some machines this forks you right back to Rule 1. Of course,
if you're working in C or C++ you have to explicitly use 'double'.
Moreover, in the embedded world I've run across at least one compiler
(TI's Code Composter for the 28xx processor) that treated the 'double'
keyword as 'please *** me over by using single precision here when I
really need double' (they did this because of Rule 1, and their
processor's 'sorta floating point' architecture that was fast with
32-bit floating point, and _not_ fast with 64-bit).
Wescott Design Services
Do you need to implement control loops in software?
"Applied Control Theory for Embedded Systems" was written for you.
See details at http://www.yqcomputer.com/