calculation question

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

Moderators: Benj, Mods

Post Reply
Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

calculation question

Post 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.
Attachments
Example1.fcfx
(8.86 KiB) Downloaded 178 times
Last edited by Dutch on Tue Oct 20, 2020 5:37 pm, edited 1 time in total.

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: calculation question

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

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: calculation question

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

User avatar
QMESAR
Valued Contributor
Valued Contributor
Posts: 1287
Joined: Sun Oct 05, 2014 3:20 pm
Location: Russia
Has thanked: 384 times
Been thanked: 614 times
Contact:

Re: calculation question

Post 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

viktor_au
Posts: 342
Joined: Fri Jan 26, 2018 12:30 pm
Location: South Australia
Has thanked: 43 times
Been thanked: 60 times
Contact:

Re: calculation question

Post by viktor_au »

Hello
I did what QMESAR suggested.
Please have a look.
You can compare strings after that.
Attachments
Example1a.fcfx
(8.96 KiB) Downloaded 177 times

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: calculation question

Post 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

Dutch
Posts: 102
Joined: Mon Aug 19, 2013 8:38 am
Location: Netherlands
Has thanked: 24 times
Been thanked: 5 times
Contact:

Re: calculation question

Post 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

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: calculation question

Post 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

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: calculation question

Post by kersing »

Have you added the local variable to the macro? You can add it in the macro properties (last tab from memory).
“Integrity is doing the right thing, even when no one is watching.”

― C.S. Lewis

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: calculation question

Post 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?
Attachments
Example1a.fcfx
(8.95 KiB) Downloaded 150 times

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: calculation question

Post 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

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times
Contact:

Re: calculation question

Post 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

Post Reply