As you can see, floating point numbers are a lot more accurate,
That's a common misconception; it's not
always true. If you
know the range of numbers you're operating on then fixed-point numbers can be actually be
more accurate than floats (at least over a range).
A (32-bit) float represents a "floating point" number like this:
Sign(1 bit)
Exponent(8 bits)
Fraction(23 bits)
This means that the maximum range of bits (accuracy) that a number can cover is 23 bits. However, the shift (exponent) does allow us to represent
very large and
very small values.
If you want to represent a range of "only" -1 to 0.99, for example, then a 32-bit fixed point system could "more accurate" than a 32-bit floating point system over parts of the range.
Floating point (worst case):
If you say the exponent makes a shift of 1, and the 23 bits are (filled to use the entire range to "disable" the exponent's utility a bit):
- Code:
100 0000 0000 0000 0000 0001
Then your maximum precision is (I think - I'm not entirely awake yet this morning):
- Code:
1/(2^22) = 0.0000002384185791015625
Of course, as the sign can be shifted around (the exponent changes and the point "floats", the accuracy range for the number goes from best case (close to 0) to worst case (23 bits of precision needed).
So, for floating point
best case, we can have a maximum accuracy of something like:
- Code:
fract expo
1/((2^22)*(2^127)) = 2^-149
= 1.4012984643248170709237295832899e-45
Fixed-point:
Sign (1 bit)
Fraction(31 bits)
If we then do the same thing, we can say that the maximum (and minimum, and average) precision is:
- Code:
1/(2^30) = 0.000000000931322574615478515625
If you were really serious about the accuracy trade-offs then you could plot a graph of fixed-point accuracy vs floating-point accuracy over the range (using something like Maple rather than something with "dubious" precision itself like Excel).
As for working with numbers like 10^-100 (I know it was only an example), I think you'll find that the granularity (accuracy) of numbers at that range, even doubles, is going to give you lots of problems with underflow.
ARM have a <a href="http://www.arm.com/support/567GAM/$File/DAI0033A_fixedpoint.pdf">document</a> on using fixed and floating point in C.
HTH,
Ref.
(Who's about to go from interesting stuff about floating-point to a friggin' management meeting. bah).