FLOAT to INT and back again.
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
- Contact:
Re: FLOAT to INT and back again.
You could try to use float for the union member in stead of the MX_FLOAT. That should result in a 4 byte float.
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
-
- Posts: 73
- Joined: Fri Jan 24, 2020 10:38 am
- Has thanked: 4 times
- Been thanked: 2 times
- Contact:
Re: FLOAT to INT and back again.
No Martin.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(and let the compiler take the strain)Code: Select all
.UL = .x * 1000
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?
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 ??
-
- Posts: 73
- Joined: Fri Jan 24, 2020 10:38 am
- Has thanked: 4 times
- Been thanked: 2 times
- Contact:
Re: FLOAT to INT and back again.
I'm not sure what you mean by this. Could you give more info please?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.
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: FLOAT to INT and back again.
You can just assign the value..
should work although you might need a cast. In a C block
-
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
Code: Select all
.f32 = .f64
-
Code: Select all
FCL_F32 = (float) FCL_F64;
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
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
- Contact:
Re: FLOAT to INT and back again.
Change your union to:
Assign the value to AsFloat and get the bytes from the 4 AsByte vast like your earlier example.
Code: Select all
typedef union
{
float AsFloat;
MX_UINT8 AsByte[4];
} MX_FloatUnion;
MX_FloatUnion FloatUnion;
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
-
- Posts: 73
- Joined: Fri Jan 24, 2020 10:38 am
- Has thanked: 4 times
- Been thanked: 2 times
- Contact:
Re: FLOAT to INT and back again.
Hi, Thanks for this. It works perfectly now.kersing wrote:Change your union to:
Assign the value to AsFloat and get the bytes from the 4 AsByte vast like your earlier example.Code: Select all
typedef union { float AsFloat; MX_UINT8 AsByte[4]; } MX_FloatUnion; MX_FloatUnion FloatUnion;
Whats the difference between the 'MX_FLOAT' and the 'float' in that one gives a 64bit value and the other gives a 32bit value.
-
- Valued Contributor
- Posts: 2045
- Joined: Wed Aug 27, 2008 10:31 pm
- Location: Netherlands
- Has thanked: 553 times
- Been thanked: 1081 times
- Contact:
Re: FLOAT to INT and back again.
MX_FLOAT expands to a double on platforms that support double precision floating point.
“Integrity is doing the right thing, even when no one is watching.”
― C.S. Lewis
― C.S. Lewis
-
- Posts: 73
- Joined: Fri Jan 24, 2020 10:38 am
- Has thanked: 4 times
- Been thanked: 2 times
- Contact:
Re: FLOAT to INT and back again.
Hi,
I have so far been using the following union to reassemble my floating point data.
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?
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;
-
- Valued Contributor
- Posts: 1189
- Joined: Wed May 31, 2017 11:57 am
- Has thanked: 70 times
- Been thanked: 439 times
- Contact:
Re: FLOAT to INT and back again.
Yes,
Use FCL_ as the prefix to the uppercase name ( instead of FCV_)
Martin
Use FCL_ as the prefix to the uppercase name ( instead of FCV_)
Martin