Working with floating point in TinyOS

There are two ways to represent float in nesc.
One way, is to do it through by convention using the exponent and mantissa,
where you need to use 2^exponent. The function that does that is called powf().
The problem with using that function is that it is reaaaaallly slow.
[QUOTE]
----8<---- span="">
DbgOut_P20_ON;
r = logf((float)v / 10000.0); // takes up about 10 ms (!!)
DbgOut_P20_OFF;
----8<---- span="">

and a
----8<---- span="">
DbgOut_P20_ON;
r = powf(10,(float)v / 10000.0); // takes up about 32 ms (!!!!!!)
DbgOut_P20_OFF;
----8<---- span="">
[/QUOTE]

In case you need to have fast response, this function might not be your choice.
One way to optimize the speed of those operations are:
"I'd like to mention that pow(a,b) is MUCH harder to implement than
exp(b). Use the latter whenever possible. In particular, if a is
fixed, use exp(b * log(a)) rather than pow(a,b). (In fact, that's a
common low-precision implementation technique even when a is not fixed.)

Also note that keeping logarithms in fixed-point format is often
convenient, and shaves a bit of overhead from the code when you
don't have hardware floating-point. "

One quite dirty trick to show real numbers in nesc is to define fix point.
for example in 16 bits, you say 12 bits are the bits before the decimal point, and 4 bits are bits after the point. a(12bits) + ( b(4bits)/16 ).
This dirty trick is good in case one wants working with float go faster, without need for much precision.

Comments

Popular posts from this blog

Timeline on Latex

Exporting Skype Chat/Skype Contacts to csv file using the shell script and sqlite3 (usually already installed on mac)

trim() not supported by IE7 and IE8