Page 2 of 2

Re: FLOAT to INT and back again.

Posted: Sat Feb 08, 2020 10:42 pm
by kersing
You could try to use float for the union member in stead of the MX_FLOAT. That should result in a 4 byte float.

Re: FLOAT to INT and back again.

Posted: Sun Feb 09, 2020 2:38 am
by Kisen
mnf wrote:So - you want to convert the value to a 32bit integer (rather than a 32bit float)?

What range of values are you working with and how many points of accuracy do you need?

Taking the byte 'values' of the fp number isn't the way to go here (it would be possible - but difficult)

Could you - for example - use

Code: Select all

.UL = .x * 1000
(and let the compiler take the strain)

Which converts (in my example above) .x (a float) to 1234 (.UL an unsigned long)

There was an interesting piece in stackoverflow about how the number of bits in the exponent and mantissa was decided upon - can't just find the link at the moment.

The Flowcode wiki describes floats as representing numbers from -infinity to +infinity - and while this is true - it might need a few more bits to hold all the numbers accurately? :D

Martin
No Martin.

At this point I would like to convert my 64bit Float into a 32bit Float and have it split into 4 bytes.

Currently I have it split successfully into 8 bytes, but this is twice the amount of data that I wanted and in my case is unnecessary.

I can only assume there is a way to shift out the 64bit data into sign, exponent and mantissa and modify it and reassemble to a 32bit Float.
It's not clear from my google searches how this is done. I'm kinda hoping that the devs at flowcode have a magic button for this ??

Re: FLOAT to INT and back again.

Posted: Sun Feb 09, 2020 2:39 am
by Kisen
kersing wrote:You could try to use float for the union member in stead of the MX_FLOAT. That should result in a 4 byte float.
I'm not sure what you mean by this. Could you give more info please?

Re: FLOAT to INT and back again.

Posted: Sun Feb 09, 2020 5:40 am
by mnf
You can just assign the value..

Code: Select all

.f32 = .f64
should work although you might need a cast. In a C block
-

Code: Select all

FCL_F32 = (float) FCL_F64;
However - if FC is defining a float as 64 bit you might struggle with your definitions?

Pulling the exponent and mantissa from the byte values would by tricky. See https://en.m.wikipedia.org/wiki/Double- ... int_format as a (brief) description of the format.
Note that 32 bit floats should form a subset of 64 bit floats - so as long as in range the assignment should work...

Martin

Re: FLOAT to INT and back again.

Posted: Sun Feb 09, 2020 9:57 am
by kersing
Change your union to:

Code: Select all

 typedef union
{
  float AsFloat;
  MX_UINT8 AsByte[4];
} MX_FloatUnion;

MX_FloatUnion FloatUnion;
Assign the value to AsFloat and get the bytes from the 4 AsByte vast like your earlier example.

Re: FLOAT to INT and back again.

Posted: Sun Feb 09, 2020 10:33 am
by Kisen
kersing wrote:Change your union to:

Code: Select all

 typedef union
{
  float AsFloat;
  MX_UINT8 AsByte[4];
} MX_FloatUnion;

MX_FloatUnion FloatUnion;
Assign the value to AsFloat and get the bytes from the 4 AsByte vast like your earlier example.
Hi, Thanks for this. It works perfectly now.
Whats the difference between the 'MX_FLOAT' and the 'float' in that one gives a 64bit value and the other gives a 32bit value.

Re: FLOAT to INT and back again.

Posted: Sun Feb 09, 2020 1:37 pm
by kersing
MX_FLOAT expands to a double on platforms that support double precision floating point.

Re: FLOAT to INT and back again.

Posted: Mon Mar 30, 2020 2:14 pm
by Kisen
Hi,

I have so far been using the following union to reassemble my floating point data.

Code: Select all

FloatUnion.AsByte[0] = FCV_TEMP_UNION_STR[0];
FloatUnion.AsByte[1] = FCV_TEMP_UNION_STR[1];
FloatUnion.AsByte[2] = FCV_TEMP_UNION_STR[2];
FloatUnion.AsByte[3] = FCV_TEMP_UNION_STR[3];
FCV_TEMP_UNION_FLOAT = FloatUnion.AsFloat;
It would be much cleaner in my code to use local variables when passing this data. Is it possible to address local variables using the C block?

Re: FLOAT to INT and back again.

Posted: Mon Mar 30, 2020 2:32 pm
by mnf
Yes,
Use FCL_ as the prefix to the uppercase name ( instead of FCV_)

Martin