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: 1930
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 568 times
Been thanked: 1078 times
Contact:

Re: FLOAT to INT and back again.

Postby kersing » Sat Feb 08, 2020 10:42 pm

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: 24
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: FLOAT to INT and back again.

Postby Kisen » Sun Feb 09, 2020 2:38 am

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: 24
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: FLOAT to INT and back again.

Postby Kisen » Sun Feb 09, 2020 2:39 am

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: 698
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 402 times
Contact:

Re: FLOAT to INT and back again.

Postby mnf » Sun Feb 09, 2020 5:40 am

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-precision_floating-point_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: 1930
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 568 times
Been thanked: 1078 times
Contact:

Re: FLOAT to INT and back again.

Postby kersing » Sun Feb 09, 2020 9:57 am

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.
These users thanked the author kersing for the post (total 3):
Kisen (Sun Feb 09, 2020 10:34 am) • medelec35 (Sun Feb 09, 2020 1:50 pm) • Benj (Mon Feb 10, 2020 1:18 pm)
Rating: 15%
 
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Kisen
Posts: 24
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: FLOAT to INT and back again.

Postby Kisen » Sun Feb 09, 2020 10:33 am

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.
These users thanked the author Kisen for the post:
Benj (Mon Feb 10, 2020 1:18 pm)
Rating: 5%
 

kersing
Valued Contributor
Valued Contributor
Posts: 1930
Joined: Wed Aug 27, 2008 10:31 pm
Location: Netherlands
Has thanked: 568 times
Been thanked: 1078 times
Contact:

Re: FLOAT to INT and back again.

Postby kersing » Sun Feb 09, 2020 1:37 pm

MX_FLOAT expands to a double on platforms that support double precision floating point.
These users thanked the author kersing for the post (total 3):
medelec35 (Sun Feb 09, 2020 1:50 pm) • LeighM (Mon Feb 10, 2020 8:45 am) • Benj (Mon Feb 10, 2020 1:18 pm)
Rating: 15%
 
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

Kisen
Posts: 24
Joined: Fri Jan 24, 2020 10:38 am
Has thanked: 3 times
Been thanked: 2 times
Contact:

Re: FLOAT to INT and back again.

Postby Kisen » Mon Mar 30, 2020 2:14 pm

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: 698
Joined: Wed May 31, 2017 11:57 am
Has thanked: 63 times
Been thanked: 402 times
Contact:

Re: FLOAT to INT and back again.

Postby mnf » Mon Mar 30, 2020 2:32 pm

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

Martin
These users thanked the author mnf for the post (total 3):
Kisen (Mon Mar 30, 2020 3:55 pm) • medelec35 (Mon Mar 30, 2020 6:47 pm) • Benj (Mon Mar 30, 2020 9:12 pm)
Rating: 15%