Page 1 of 1

Conversion rules float to/from unsigned integer

Posted: Tue Jun 20, 2017 9:52 am
by jadiflow
Hi,

My project requires conversion of (unsigned)integer to floating point and back. I've seen some anomalies in equations where I thought the conversion was inplicit but did not give the expected result.
I perused the Help file but it only gives rules for floating point operations as such, no conversion rules.
Are these available somewhere?

While I am at it: is it correct that the rounding function fround is not available for 16Fxxx PIC processors?

Jan Didden

Re: Conversion rules float to/from unsigned integer

Posted: Tue Jun 20, 2017 10:15 am
by Benj
Hi Jan,
is it correct that the rounding function fround is not available for 16Fxxx PIC processors?
Yes this is not available for 8-bit PICs in Flowcode v5 and v6, it is available via the new compiler used in v7.

What sort of resolution are you looking for from the float to int conversion? it should be possible to do something like this.

intwhole = float2int( float )
floattemp = float - intwhole
floattemp = floattemp * 1000
intreal = float2int( floattemp )

Re: Conversion rules float to/from unsigned integer

Posted: Tue Jun 20, 2017 2:57 pm
by jadiflow
Thanks Ben, that is very helpful. I thought that something like fSomething = fSomethingElse + uiNumber would implicitly convert the ui before doing the operation but it is not consistent.
I was not aware of the float2int function.
Can I assume there is also an int2float? Is there a list of these functions somewhere?

I will check out FC7 for te fround; in fact I think I have upgraded to FC7 (and FC6 before that) but I am so used to FC5 that it's hard to say goodby to it...;-)

Jan

Re: Conversion rules float to/from unsigned integer

Posted: Tue Jun 20, 2017 3:21 pm
by Benj
Hi Jan,

Yes there is an int2float and the functions can be found in the Fx tab of the calculation icon properties.
CalcFunctions.jpg
CalcFunctions.jpg (41.69 KiB) Viewed 11820 times
V6 and v7 take a bit of getting used to (v7 less so than v6) but I vastly prefer them to v5 now I've gotten used to them.

Re: Conversion rules float to/from unsigned integer

Posted: Wed Jun 21, 2017 12:03 pm
by jadiflow
I bit the bullet and upgraded to FC7 ;-)

But I am getting stuck with the Supp Code for EEPROM presetting. Would be nice if you could take a look at my latest post in the FC7 forum.

Re: Conversion rules float to/from unsigned integer

Posted: Wed Jun 21, 2017 12:14 pm
by Benj
Congratulations, let us know how you get on.

I've replied to your other topic now.

Re: Conversion rules float to/from unsigned integer

Posted: Tue Sep 15, 2020 3:39 pm
by AbhijitR
Hello! Ben
good afternoon
Benj wrote:
Tue Jun 20, 2017 10:15 am
intwhole = float2int( float )
floattemp = float - intwhole
floattemp = floattemp * 1000
intreal = float2int( floattemp )
May I request you to help me with this conversion, I tried to do as mentioned above but without success.

I am able to increment/decrement the float variable, but cannot convert the float(100.5) back to integer along with the decimal value with the above calculation.

I created the variables as below
intwhole as a Integer variable
float as Float variable
floattemp as Float variable
intreal as Integer variable

Something wrong I think, can you correct?

Thank you.

Abhi

Edit: I just noticed after posting, I post in V5 while I am using V8, need to change the version? kindly correct.

Re: Conversion rules float to/from unsigned integer

Posted: Tue Sep 15, 2020 4:04 pm
by Benj
Hi Abhi,

Can you post your program and we will have a look for you.

the following lines might be better as

floattemp = float - FLOAT intwhole
floattemp = floattemp * 1000.0

Re: Conversion rules float to/from unsigned integer

Posted: Tue Sep 15, 2020 4:46 pm
by AbhijitR
Hi! Ben

Thanks for the reply, excuse me for the delay, it took some time to take out the required from the main chart.

I check the chart with F8, on every count the UP key will increment the count by 0.1 to 100 (and also decrement well), after five loop the count will be 100.5, I want to save the 100.5 to the Temporary_Variable pressing the enter key, I tried as you explained in the post, but I think I am making some mistake.

Thank you.

Abhi

Re: Conversion rules float to/from unsigned integer

Posted: Tue Sep 15, 2020 6:07 pm
by Bachman
Don't use FLOAT, you don't need it.

If you have a number what is needed to be increment or decrement by 0.1, multiply everything by 10. In this case, 1004 means 100.4 You have to make some tricks only if you want to display it as a Float.

x=1004 (the original number, means 100.4)
y=x mod 10 (y will be 4)
x=x/10 (x will be 100)

Display x, display decimal point, display y.

Yes, using division require lots of steps, it takes time but at the end, you can spare tons of RAM. If the process is slow, increase the speed of the controller. I don't now but I think, calculating with FLOAT also takes lots of steps/times.

Re: Conversion rules float to/from unsigned integer

Posted: Tue Sep 15, 2020 6:24 pm
by AbhijitR
Hello! Bachman

Thanks for your answer, I will try to understand and implement your suggestion, I also need to save the value in EEPROM.

Thank you again.

Abhi