'float - is it necessary? (was Re: Other MicroContr'
|> From: Walter Banks <BYTECRAFT.COM> walter
> Werner Terreblanche wrote:
> > To put this to a test, try finding a PIC C compilers that can do
> > floating point arithmetic.
> This is an unusual test for a C compiler. I have seen very few
> applications that would use a Microchip PIC class of processor
> where floating point support is a requirement.
I have to agree with Walter. What would be much, much more useful
for a PIC compiler would be the ability to specify various fixed
point formats and the compiler then takes care of optimum scaling
of intermediate results. If using C, then perhaps the following
typedef fixed 8:3 unsigned int foo;
typedef fixed 15:15 signed int bar;
X = A * B; /* Throwing away sign of B */
Y = A + B; /* Result truncation */
Y = B * B; /* OK */
'fixed n:m' is a type qualifier (somewhat similar to 'long' or the
abominable 8086 pointer qualifiers) where n is the number of significant
bits, and m is the number of those bits after the implied binary point.
Thus a foo type represents a number between 0 and 7 7/8 and bar
represents -1 to +32767/32768, if using 2's complement. Now if
a foo is multiplied by a bar, then the compiler will realise that
the result will have 8 significant bits (the smaller of the operands)
and 3 of those bits will be fractional. However the result is unsigned type
so the compiler will warn about loss of sign. If the code assigns the
result to a value with a smaller number of significant bits, or
a badly positioned binary point, then a warning will be issued in
which the compiler tells the user that significance loss or truncation
is occuring, and suggests a more appropriate type.
This leaves most of the thinking about the problem to the user,
with the compiler implementing the arithmetic in the most efficient
way and warning the user if something doesn't make sense.
Naturally, up to 32-bit precision should be supported. (Then I
could implement my julian day calendar!)
This shouldn't be too hard for the compiler gurus to implement,
More... (looser matching)
- Last day of these posts
- In 1997
, 1998 only
- New search...