FLOAT to INT and back again.

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

kersing
Valued Contributor
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.

Post 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.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Kisen
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.

Post 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 ??

Kisen
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.

Post 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?

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: FLOAT to INT and back again.

Post 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

kersing
Valued Contributor
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.

Post 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.
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Kisen
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.

Post 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.

kersing
Valued Contributor
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.

Post by kersing »

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

Kisen
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.

Post 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?

mnf
Valued Contributor
Valued Contributor
Posts: 1188
Joined: Wed May 31, 2017 11:57 am
Has thanked: 70 times
Been thanked: 439 times
Contact:

Re: FLOAT to INT and back again.

Post by mnf »

Yes,
Use FCL_ as the prefix to the uppercase name ( instead of FCV_)

Martin

Post Reply