> One way to "print" a floating point number might be:
> printf("%d.%03d", fp, (fp - (int)fp) * 100);
> YMMV: note the issues with negative numbers and rounding errors.
And, as you can see, buggy, both params need an addition cast to an
integer type (right now they are passed as floats).
Also, please bear in mind that FP is an expensive operation and, in
many cases, you can write integer-based code to avoid using it**.
Incredibly expensive if it is emulated. Emulation will also increase
the size of the ROM. I do not know which platforms have native FP and
which do not :)
**If you just need "precision" in a much smaller range, you can take
the same approach as say, timers, assuming a Milli timer, 1024ms = 1s,
therefore, 1.5s = 1536millis; you can get away with a good bit of math
(which is relatively cheap) by multiplying by an integer and then
dividing by a power of two (this allows the shift op-code to be used).
e.g., to multiply x, imagine is "1 second" (1 * 1024, for TMilli), by
1.875 (15/8): x *= 7; x <<= 3; which yields: 1920 (or "1.875 seconds",
with TMilli). One thing to watch out for with this approach though, is
integer overflow errors during the "multiply" stage (you will likely
need to cast up from int8, and maybe even int16, to int16 and/or
int32, respectively).
Reasons to stay with/use an exponent based FP implementation is that
it can cover a much wider range (albeit it loses precision the further
out you go and can never entirely [accurately] represent a wide range
of discreet numbers) and NaN/Inf can be encoded-- are these needed for
*your* mote?
Paul
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help