Page 1 of 1

calculation question

Posted: Tue Oct 20, 2020 4:38 pm
by Dutch
Hi all,

Im having an maybe simple question. I cant figure it out. I devide in a flow calculation two local variables. Say .data109=1808 and .data129=5932.

So the calculation is for example then .data200= .data129/.data109 that result is 3,28097345
The min and max I have in real lies inbetween 1,2xxxxxxx and 3,5xxxxxxx.
I need to run the result trough a number of decision's. I thought to simply multiply the result 1,xxxx to 3,xxxx by 10000. But the number behind the decimal point is not multiplied. the result is then 10000, 20000 or 30000.

what do I need to do to get a 16bit (max 65535) number? how do I 'shift' the decimal point?

Thanks for your helpful answer!
Angelo.

Re: calculation question

Posted: Tue Oct 20, 2020 5:33 pm
by QMESAR
Hi
data200= .data129/.data109 t
Is the data200 of type Integer or float? if it is type integer or unsigned integer you loose the decimal part by the nature of the variable type is my guess,
if data200 is type float then you will see the vale of 1-3.xxx you can then do the multiplication by 10 000 and convert float to int then you have the 16bit number ,

Re: calculation question

Posted: Tue Oct 20, 2020 5:43 pm
by Dutch
Thank you QMESAR for thinking along! I edited my post with an simple example in the attachment. in the property's I cant change it to float if you mean that.

Re: calculation question

Posted: Tue Oct 20, 2020 6:21 pm
by QMESAR
No Problem ,I had a quick look and saw you are using local variables ,I am not so well informed with this in FC
maybe Martin or someone that works a lot with FC can help you here ,I know how to solve this in C code but that helps you nothing

Re: calculation question

Posted: Tue Oct 20, 2020 10:31 pm
by viktor_au
Hello
I did what QMESAR suggested.
Please have a look.
You can compare strings after that.

Re: calculation question

Posted: Tue Oct 20, 2020 11:21 pm
by mnf
I'm not sure I follow the question - but the strings are reported as 3.28 and 3280 because the string variables are only 4 bytes long - so a maximum of 4 digits...

Change the strings to 'say' 10 bytes for more 'resolution' - note also that FloatToStr$ takes an optional second argument (default value 6) which defines the number of digits after the decimal point to include in the result (space in the string permitting) - though 6 is probably about the maximum for 32 bit floats...

Martin

Re: calculation question

Posted: Wed Oct 21, 2020 9:01 am
by Dutch
Wow! thank you Qmesar and Viktor for all, and Martin for the remark! and the quick response.

I have merged it into my code and it works way more refined now :D . 4 digits seems enough. I could go to 5 so it still fits in the 0-65535 range.

il try to remember the trick but I have it 'stored'... every time something new!

Regards Angelo

Re: calculation question

Posted: Wed Oct 21, 2020 7:45 pm
by jgu1
Hi!

I see in Victor´s example lokal variable. I try to create a new project similar with .Testvar but it says error when I try to compile. Is there something I have to add or do to create a local variable., I have tryed to search here and in Wiki without luck. please.

Br. Jorgen

Re: calculation question

Posted: Wed Oct 21, 2020 8:03 pm
by kersing
Have you added the local variable to the macro? You can add it in the macro properties (last tab from memory).

Re: calculation question

Posted: Wed Oct 21, 2020 8:24 pm
by jgu1
Hi Kersing!

In the macro, please have patience with me. :wink: If I look in the example I dont see any macro.

Edit: Well now I found it, thank´s. By the way, if using local vs the other variabel, does it save space?

Re: calculation question

Posted: Wed Oct 21, 2020 9:13 pm
by mnf
Using a local can save space - global variables exist for the entire time the program is running - they take a fixed amount of space. Local variables exist only during the macro call they are'local' too and are usually allocated on a stack.

So there are several reasons to use local variables - imagine ab large program with hundreds of global variables - these can take up a lot of memory.
Also it can be very hard to keep track of which variable is used for a particular use - making the program harder to understand.
It also increases the risk of side effects - a variable value can be changed by a different part of the program giving unexpected results.

So globals should ideally be limited to a few state/program wide values.

Martin

Martin

Re: calculation question

Posted: Wed Oct 21, 2020 9:31 pm
by jgu1
Wow Martin. Thank you and Kersing for a good explanation and help. I learn again some news today. I will try to work wit this.
Amazing what we can with Flowcode :wink:

Br Jorgen