Hi,
An alternative to the casts would be to use a decimal point:
float a;
a = (8.0-3.0)/(9.0-1.0);
It seems that on the MicaZ and TelosB platforms there is no difference
between float and double, both are 32-bit floating point numbers.
To send floating point numbers over the network, I use the following
approach:
// ----------
// in the header file
typedef nx_struct FloatMsg {
nx_uint32_t number;
} FloatMsg;
// to send data
void sendMethod() {
uint32_t temp;
FloatMsg msg;
//...
*(float*)&temp = 1.234;
msg.number = temp;
//...
}
// receiving data
event /*..*/ Net.receive(message_t *msg, void *payload, uint8_t len) {
uint32_t temp;
FloatMsg* flt = (FloatMsg*)msg;
float number;
//...
temp = flt->number;
number = *(float*)&temp;
//...
}
// ----------
In Java:
// ----------
public void messageReceived(int to, Message msg) {
FloatMsg flt = (FloatMsg)msg;
float number = Float.intBitsToFloat(flt.get_number());
//...
}
public void sendNumber() {
FloatMsg flt = ...;
flt.set_number(Float.floatToIntBits(1.234f));
//...
}
// ----------
Does anybody have a better approach? Is somebody implementing something
like nx_float or nx_double?
Cheers,
Urs
Hussein Khaleel wrote:
> Hi,
> In order to assign a float value to a variable, you basically have to
> CAST everything that is not float in your equation. For example:
>
> float a;
> a = (8-3)/(9-1); you'll get 1 (rounded)
>
> But
>
> float a;
> a = (float)(8-3)/(float)(9-1); you'll get 0.625
>
> One more thing is the accuracy of the float. You'll get a maximum of 8
> digits in total, before and after the "point".
> For example, if you have this value 123.45678912 you'll get 123.45679
> (total of 9 digits).
> Good luck, I hope it works for you.
> Hussein
>
> Adeel Akhtar wrote:
>> Hi All
>> can anybody tell me how to handle float numbers in nesc language?
>>
>> --
>>
>>
>> Thanks & Best Regards
>>
>> Adeel Akhtar
_______________________________________________
Tinyos-help mailing list
[email protected]
https://www.millennium.berkeley.edu/cgi-bin/mailman/listinfo/tinyos-help