Mathematical operations with variables and their result.

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Mathematical operations with variables and their result.

#1 Post by igorka » 19 Jan 2023, 15:49

Until recently, I had no need for calculations and therefore did not know that an unpleasant surprise awaited me :( . Next, I will give examples of what you need to pay attention to and whether such calculation logic will suit you. I do not rule out that for many this will not be a problem, but everyone should know and remember this.

Code: Select all

 
 let,1,9
 let,2,2
 let,3,[int#1]/[int#2]
Got the following result:
let,3,4.5 If you take any calculator, then it will calculate us exactly the same 9/2=4.5. Here it is worth paying attention to the fact that if you work with variables of type string, this result will be correct (actually, by default, all variables are of type string), but the same operation with variables of type int16 will give us the result 4! This is actually what caused my frustration. It seemed to me that the available type of variables is the string %***% and [int #*** ], it is not. In general, those who are used to working with integer logic will understand, and those who are not, this is unnecessary.
To somehow get out of this situation, you can do this, cut off everything after the comma:

Code: Select all

let,3,[int#1]/[int#2]
let,3,[int#3]
And the result is still unpredictable let,3,5, but not let,3,4... Instead of truncating the 5 after the decimal point, the 4 in the first decimal place is rounded to 5! I can assume that this was done for the convenience of people who are completely unfamiliar with programming, but for those who understand at least a little bit, such a developer's decision creates difficulties.
The same problems when trying to get rid of the comma by multiplying by 10. An example is below:

Code: Select all

let,1,22.4//for example this variable has the temperature value, Environment - DS18b20
let,2,[int#1]*10
The result is 220, not 224! This is where measurement accuracy is lost. But if you do this in the formula %value%*10, then everything is in order, only you can’t convert it back in the rules using division by 10!
I hope that in the future, developers will add variability in the choice of data type and ensure correct work with them. ;)

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Mathematical operations with variables and their result.

#2 Post by TD-er » 19 Jan 2023, 16:12

You can also use the [var#N] notation, as this is addressing the same variable as [int#N], only the latter is rounded to int (not cast to int).
That's where your 'frustration' probably comes from.

Apart from it being 'strange' from a programmer's standpoint, I'm not really sure yet which use case is no longer possible with this behavior.

Using either the [int#N] or [var#N] notation you should still be able to use the formatting options as mentioned here:
https://espeasy.readthedocs.io/en/lates ... red-values

Not yet tested, but I think this should work too:
[var#1#F] to floor it as it would be done when using integer computation you're used to.

Or in your example to get an integer representation in deci-units (1/10th of a degree resolution for example)

let,1,[var#1#D.1]*10

(N.B. not tested)

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Mathematical operations with variables and their result.

#3 Post by igorka » 19 Jan 2023, 16:38

Thank you.
There is good news. :)
The recording works as it should:

Code: Select all

let,1,26.6
let,1,[var#1]*10
//
let,1,266//result
//
let,1,[var#1]/10
//
let,1,26.6//result
This entry works too:

Code: Select all

let,1,26.6
let,1,[var#1#F]
let,1,26//result
This entry works too:

Code: Select all

let,1,26.6
let,1,[var#1#D.1]*10
let,1,266//result

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Mathematical operations with variables and their result.

#4 Post by igorka » 22 Jan 2023, 11:51

There was a following problem and lack of its decision. So I'm asking for advice and help.
It is worth mentioning that the code contains and repeats "redundant-superfluous" lines of code, this is done in order to deliberately exclude the influence of commas in numbers. I assigned values to all variables in System # Boot (I know that a variable without a declaration has a value of 0 by default), for me it is more familiar and readability is increased.
The structure that works correctly:

Code: Select all

On System#Boot do
  let,1,0
  let,2,0
  let,3,100
  let,4,5
  let,5,1
  let,6,0
  let,7,1
  let,8,0
  let,9,500
  let,10,0
  let,11,0
  let,12,0
  let,13,0
  let,14,0
loopTimerSet,1,1
endon
on Rules#Timer=1 do
  let,8,48.5
  let,8,[var#8]*10
  let,9,[var#9#F]
  let,10,[int#9]-[int#8]
  let,10,[var#10#F]
  let,12,[int#10]-[int#13]
  let,12,[var#12#F]
  let,13,[int#10]
  let,13,[var#13#F]
  let,14,[int#10]*[int#4]
  let,14,[var#14#F]
  let,14,[int#14]+([int#11]*[int#5])/[int#1]
  let,14,[var#14#F]
  let,14,[int#14]+([int#12]*[int#6])
  let,14,[var#14#F]
  let,14,[int#14]+[int#12]
  let,14,[var#14#F]
  let,14,[int#14]/[int#7]
  let,14,[var#14#F]
  if [int#14] > 100
  let,14,100
  else 
  if [int#14] < 0
  let,14,0
  else
  let,11,[int#11]+[int#10]
  let,11,[var#11#F]
  endif
publish,PID_y/output,[int#14]
endon

Code: Select all

58449: ACT  : let,13,5
58453: ACT  : let,13,5
58456: ACT  : let,14,5*5
58459: ACT  : let,14,25
58463: ACT  : let,14,25+(285*1)/25
58467: ACT  : let,14,36
58471: ACT  : let,14,36+(0*0)
58475: ACT  : let,14,36
58478: ACT  : let,14,36+0
58482: ACT  : let,14,36
58485: ACT  : let,14,36/1
58488: ACT  : let,14,36
58500: ACT  : let,11,285+5
58504: ACT  : let,11,290
58508: ACT  : publish,PID_y/output,36
60452: ACT  : let,13,5
60455: ACT  : let,13,5
60458: ACT  : let,14,5*5
60462: ACT  : let,14,25
60465: ACT  : let,14,25+(295*1)/25
60470: ACT  : let,14,36
60474: ACT  : let,14,36+(0*0)
60478: ACT  : let,14,36
60481: ACT  : let,14,36+0
60484: ACT  : let,14,36
60488: ACT  : let,14,36/1
60491: ACT  : let,14,36
60501: ACT  : let,11,295+5
60505: ACT  : let,11,300
60509: ACT  : publish,PID_y/output,36
61453: ACT  : let,13,5
61456: ACT  : let,14,5*5
61459: ACT  : let,14,25
61463: ACT  : let,14,25+(300*1)/25
61468: ACT  : let,14,37
61472: ACT  : let,14,37+(0*0)
61476: ACT  : let,14,37
61480: ACT  : let,14,37+0
61483: ACT  : let,14,37
61487: ACT  : let,14,37/1
61490: ACT  : let,14,37
61500: ACT  : let,11,300+5
61504: ACT  : let,11,305
61508: ACT  : publish,PID_y/output,37
61757: WD   : Uptime 1 ConnectFailures 0 FreeMem 20696 WiFiStatus 3 ESPeasy internal wifi status: Conn. IP Init
62449: ACT  : let,13,5
62452: ACT  : let,13,5
62456: ACT  : let,14,5*5
62459: ACT  : let,14,25
62463: ACT  : let,14,25+(305*1)/25
62467: ACT  : let,14,37
62471: ACT  : let,14,37+(0*0)
62475: ACT  : let,14,37
62478: ACT  : let,14,37+0
62481: ACT  : let,14,37
62485: ACT  : let,14,37/1
62489: ACT  : let,14,37
62500: ACT  : let,11,305+5
62507: ACT  : let,11,310
62514: ACT  : publish,PID_y/output,37
77451: ACT  : let,13,5
77454: ACT  : let,13,5
77458: ACT  : let,14,5*5
77461: ACT  : let,14,25
77465: ACT  : let,14,25+(380*1)/25
77469: ACT  : let,14,40
77473: ACT  : let,14,40+(0*0)
77477: ACT  : let,14,40
77480: ACT  : let,14,40+0
77483: ACT  : let,14,40
77487: ACT  : let,14,40/1
77490: ACT  : let,14,40
77500: ACT  : let,11,380+5
77504: ACT  : let,11,385
77508: ACT  : publish,PID_y/output,40
If you change the value in a variable for example let,8,45.5. publish,PID_y/output,[int#14] stops working and other manipulations with the variable become impossible, as if there is no such variable at all. Miracles...
Non-working code:

Code: Select all

On System#Boot do
  let,1,0
  let,2,0
  let,3,100
  let,4,5
  let,5,1
  let,6,0
  let,7,1
  let,8,0
  let,9,500
  let,10,0
  let,11,0
  let,12,0
  let,13,0
  let,14,0
loopTimerSet,1,1
endon
on Rules#Timer=1 do
  let,8,45.5//value with which it stops working
  let,8,[var#8]*10
  let,9,[var#9#F]
  let,10,[int#9]-[int#8]
  let,10,[var#10#F]
  let,12,[int#10]-[int#13]
  let,12,[var#12#F]
  let,13,[int#10]
  let,13,[var#13#F]
  let,14,[int#10]*[int#4]
  let,14,[var#14#F]
  let,14,[int#14]+([int#11]*[int#5])/[int#1]
  let,14,[var#14#F]
  let,14,[int#14]+([int#12]*[int#6])
  let,14,[var#14#F]
  let,14,[int#14]+[int#12]
  let,14,[var#14#F]
  let,14,[int#14]/[int#7]
  let,14,[var#14#F]
  if [int#14] > 100
  let,14,100
  else 
  if [int#14] < 0
  let,14,0
  else
  let,11,[int#11]+[int#10]
  let,11,[var#11#F]
  endif
publish,PID_y/output,[int#14]
endon

Code: Select all

4445: ACT  : let,12,45-45
4448: ACT  : let,12,0
4451: ACT  : let,13,45
4455: ACT  : let,13,45
4458: ACT  : let,14,45*5
4462: ACT  : let,14,225
4465: ACT  : let,14,225+(0*1)/25
4469: ACT  : let,14,225
4473: ACT  : let,14,225+(0*0)
4477: ACT  : let,14,225
4480: ACT  : let,14,225+0
4484: ACT  : let,14,225
4489: ACT  : let,14,225/1
4493: ACT  : let,14,225
4498: ACT  : let,14,100
8445: ACT  : let,12,45-45
8448: ACT  : let,12,0
8451: ACT  : let,13,45
8454: ACT  : let,13,45
8458: ACT  : let,14,45*5
8461: ACT  : let,14,225
8465: ACT  : let,14,225+(0*1)/25
8469: ACT  : let,14,225
8473: ACT  : let,14,225+(0*0)
8477: ACT  : let,14,225
8481: ACT  : let,14,225+0
8486: ACT  : let,14,225
8489: ACT  : let,14,225/1
8492: ACT  : let,14,225
8498: ACT  : let,14,100
9445: ACT  : let,12,45-45
9448: ACT  : let,12,0
9451: ACT  : let,13,45
9455: ACT  : let,13,45
9458: ACT  : let,14,45*5
9462: ACT  : let,14,225
9465: ACT  : let,14,225+(0*1)/25
9469: ACT  : let,14,225
9473: ACT  : let,14,225+(0*0)
9477: ACT  : let,14,225
9480: ACT  : let,14,225+0
9484: ACT  : let,14,225
9487: ACT  : let,14,225/1
9491: ACT  : let,14,225
9496: ACT  : let,14,100
10451: ACT  : let,12,45-45
10454: ACT  : let,12,0
10457: ACT  : let,13,45
10461: ACT  : let,13,45
10464: ACT  : let,14,45*5
10468: ACT  : let,14,225
10471: ACT  : let,14,225+(0*1)/25
10475: ACT  : let,14,225
10479: ACT  : let,14,225+(0*0)
10483: ACT  : let,14,225
10487: ACT  : let,14,225+0
10490: ACT  : let,14,225
10493: ACT  : let,14,225/1
10497: ACT  : let,14,225
10502: ACT  : let,14,100
11445: ACT  : let,12,45-45
11450: ACT  : let,12,0
11453: ACT  : let,13,45
11457: ACT  : let,13,45
11460: ACT  : let,14,45*5
11464: ACT  : let,14,225
11467: ACT  : let,14,225+(0*1)/25
11471: ACT  : let,14,225
11475: ACT  : let,14,225+(0*0)
11479: ACT  : let,14,225
11482: ACT  : let,14,225+0
11486: ACT  : let,14,225
11489: ACT  : let,14,225/1
11493: ACT  : let,14,225
11498: ACT  : let,14,100
12445: ACT  : let,12,45-45
12449: ACT  : let,12,0
12452: ACT  : let,13,45
12455: ACT  : let,13,45
12459: ACT  : let,14,45*5
12462: ACT  : let,14,225
12466: ACT  : let,14,225+(0*1)/25
12470: ACT  : let,14,225
12475: ACT  : let,14,225+(0*0)
12479: ACT  : let,14,225
12483: ACT  : let,14,225+0
12487: ACT  : let,14,225
12490: ACT  : let,14,225/1
12493: ACT  : let,14,225
12499: ACT  : let,14,100
13445: ACT  : let,12,45-45
13449: ACT  : let,12,0
13451: ACT  : let,13,45
13455: ACT  : let,13,45
13458: ACT  : let,14,45*5
13462: ACT  : let,14,225
13466: ACT  : let,14,225+(0*1)/25
13470: ACT  : let,14,225
13473: ACT  : let,14,225+(0*0)
13477: ACT  : let,14,225
13480: ACT  : let,14,225+0
13484: ACT  : let,14,225
13487: ACT  : let,14,225/1
13491: ACT  : let,14,225
13497: ACT  : let,14,100
14447: ACT  : let,12,45-45
14450: ACT  : let,12,0
14453: ACT  : let,13,45
14457: ACT  : let,13,45
14460: ACT  : let,14,45*5
14464: ACT  : let,14,225
14467: ACT  : let,14,225+(0*1)/25
14471: ACT  : let,14,225
14475: ACT  : let,14,225+(0*0)
14479: ACT  : let,14,225
14482: ACT  : let,14,225+0
14486: ACT  : let,14,225
14489: ACT  : let,14,225/1
14493: ACT  : let,14,225
14498: ACT  : let,14,100
16445: ACT  : let,12,45-45
16448: ACT  : let,12,0
16451: ACT  : let,13,45
16455: ACT  : let,13,45
16458: ACT  : let,14,45*5
16462: ACT  : let,14,225
16468: ACT  : let,14,225+(0*1)/25
16476: ACT  : let,14,225
16482: ACT  : let,14,225+(0*0)
16486: ACT  : let,14,225
16490: ACT  : let,14,225+0
16493: ACT  : let,14,225
16496: ACT  : let,14,225/1
16500: ACT  : let,14,225
16505: ACT  : let,14,100
17445: ACT  : let,12,45-45
17448: ACT  : let,12,0
17451: ACT  : let,13,45
17455: ACT  : let,13,45
17458: ACT  : let,14,45*5
17462: ACT  : let,14,225
17466: ACT  : let,14,225+(0*1)/25
17470: ACT  : let,14,225
17473: ACT  : let,14,225+(0*0)
17477: ACT  : let,14,225
17481: ACT  : let,14,225+0
17484: ACT  : let,14,225
17487: ACT  : let,14,225/1
17492: ACT  : let,14,225
17498: ACT  : let,14,100
18445: ACT  : let,12,45-45
18448: ACT  : let,12,0
18451: ACT  : let,13,45
18455: ACT  : let,13,45
18458: ACT  : let,14,45*5
18462: ACT  : let,14,225
18466: ACT  : let,14,225+(0*1)/25
18470: ACT  : let,14,225
18478: ACT  : let,14,225+(0*0)
18483: ACT  : let,14,225
18486: ACT  : let,14,225+0
18490: ACT  : let,14,225
18493: ACT  : let,14,225/1
18497: ACT  : let,14,225
18502: ACT  : let,14,100
19447: ACT  : let,12,45-45
19450: ACT  : let,12,0
19453: ACT  : let,13,45
19457: ACT  : let,13,45
19460: ACT  : let,14,45*5
19464: ACT  : let,14,225
19468: ACT  : let,14,225+(0*1)/25
19472: ACT  : let,14,225
19475: ACT  : let,14,225+(0*0)
19479: ACT  : let,14,225
19483: ACT  : let,14,225+0
19486: ACT  : let,14,225
19490: ACT  : let,14,225/1
19493: ACT  : let,14,225
19499: ACT  : let,14,100
20448: ACT  : let,12,45-45
20454: ACT  : let,12,0
20457: ACT  : let,13,45
20462: ACT  : let,13,45
20466: ACT  : let,14,45*5
20470: ACT  : let,14,225
20474: ACT  : let,14,225+(0*1)/25
20478: ACT  : let,14,225
20482: ACT  : let,14,225+(0*0)
20486: ACT  : let,14,225
20489: ACT  : let,14,225+0
20493: ACT  : let,14,225
20496: ACT  : let,14,225/1
20500: ACT  : let,14,225
20505: ACT  : let,14,100
21445: ACT  : let,12,45-45
21448: ACT  : let,12,0
21451: ACT  : let,13,45
21454: ACT  : let,13,45
21458: ACT  : let,14,45*5
21462: ACT  : let,14,225
21466: ACT  : let,14,225+(0*1)/25
21470: ACT  : let,14,225
21474: ACT  : let,14,225+(0*0)
21478: ACT  : let,14,225
21481: ACT  : let,14,225+0
21486: ACT  : let,14,225
21489: ACT  : let,14,225/1
21493: ACT  : let,14,225
21498: ACT  : let,14,100
22445: ACT  : let,12,45-45
22448: ACT  : let,12,0
22451: ACT  : let,13,45
22454: ACT  : let,13,45
22458: ACT  : let,14,45*5
22461: ACT  : let,14,225
22465: ACT  : let,14,225+(0*1)/25
22470: ACT  : let,14,225
22473: ACT  : let,14,225+(0*0)
22477: ACT  : let,14,225
22480: ACT  : let,14,225+0
22484: ACT  : let,14,225
22487: ACT  : let,14,225/1
22491: ACT  : let,14,225
22496: ACT  : let,14,100
23447: ACT  : let,12,45-45
23450: ACT  : let,12,0
23453: ACT  : let,13,45
23457: ACT  : let,13,45
23460: ACT  : let,14,45*5
23463: ACT  : let,14,225
23467: ACT  : let,14,225+(0*1)/25
23472: ACT  : let,14,225
23475: ACT  : let,14,225+(0*0)
23479: ACT  : let,14,225
23483: ACT  : let,14,225+0
23486: ACT  : let,14,225
23490: ACT  : let,14,225/1
23493: ACT  : let,14,225
23499: ACT  : let,14,100
24445: ACT  : let,12,45-45
24448: ACT  : let,12,0
24451: ACT  : let,13,45
24456: ACT  : let,13,45
24460: ACT  : let,14,45*5
24463: ACT  : let,14,225
24467: ACT  : let,14,225+(0*1)/25
24471: ACT  : let,14,225
24475: ACT  : let,14,225+(0*0)
24479: ACT  : let,14,225
24483: ACT  : let,14,225+0
24486: ACT  : let,14,225
24490: ACT  : let,14,225/1
24493: ACT  : let,14,225
24499: ACT  : let,14,100
25445: ACT  : let,12,45-45
25448: ACT  : let,12,0
25451: ACT  : let,13,45
25455: ACT  : let,13,45
25458: ACT  : let,14,45*5
25461: ACT  : let,14,225
25465: ACT  : let,14,225+(0*1)/25
25469: ACT  : let,14,225
25473: ACT  : let,14,225+(0*0)
25477: ACT  : let,14,225
25482: ACT  : let,14,225+0
25486: ACT  : let,14,225
25489: ACT  : let,14,225/1
25493: ACT  : let,14,225
25498: ACT  : let,14,100
26445: ACT  : let,12,45-45
26448: ACT  : let,12,0
26451: ACT  : let,13,45
26455: ACT  : let,13,45
26458: ACT  : let,14,45*5
26461: ACT  : let,14,225
26468: ACT  : let,14,225+(0*1)/25
26474: ACT  : let,14,225
26478: ACT  : let,14,225+(0*0)
26483: ACT  : let,14,225
26486: ACT  : let,14,225+0
26490: ACT  : let,14,225
26493: ACT  : let,14,225/1
26497: ACT  : let,14,225
26502: ACT  : let,14,100
27446: ACT  : let,12,45-45
27450: ACT  : let,12,0
27453: ACT  : let,13,45
27456: ACT  : let,13,45
27459: ACT  : let,14,45*5
27463: ACT  : let,14,225
27467: ACT  : let,14,225+(0*1)/25
27471: ACT  : let,14,225
27475: ACT  : let,14,225+(0*0)
27478: ACT  : let,14,225
27482: ACT  : let,14,225+0
27485: ACT  : let,14,225
27489: ACT  : let,14,225/1
27493: ACT  : let,14,225
27498: ACT  : let,14,100
28445: ACT  : let,12,45-45
28450: ACT  : let,12,0
28453: ACT  : let,13,45
28457: ACT  : let,13,45
28460: ACT  : let,14,45*5
28464: ACT  : let,14,225
28467: ACT  : let,14,225+(0*1)/25
28471: ACT  : let,14,225
28475: ACT  : let,14,225+(0*0)
28479: ACT  : let,14,225
28482: ACT  : let,14,225+0
28486: ACT  : let,14,225
28490: ACT  : let,14,225/1
28493: ACT  : let,14,225
28499: ACT  : let,14,100

User avatar
Ath
Normal user
Posts: 3415
Joined: 10 Jun 2018, 12:06
Location: NL

Re: Mathematical operations with variables and their result.

#5 Post by Ath » 22 Jan 2023, 12:07

An 'endif' is missing from your 'on rules#timer=1 do' rule, not sure where it should go, but that is a very likely cause for erratic code behavior.
Finding such errors is much easier when using systematic indenting of your code.
/Ton (PayPal.me)

TD-er
Core team member
Posts: 8642
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Mathematical operations with variables and their result.

#6 Post by TD-er » 22 Jan 2023, 12:43

Also you can easily view the current value of all variables on the sysvars page.
I find it hard to understand your code without any comments.

Indeed there is an endif missing in the last part of your rules:

Code: Select all

...
  if [int#14] > 100
    let,14,100
  else 
    if [int#14] < 0
      let,14,0
    else
      let,11,[int#11]+[int#10]
      let,11,[var#11#F]
  endif
  publish,PID_y/output,[int#14]
endon
Also you modify int 14 only if it is outside the range of 0...100, but in the else you modify int 11, without using it.
You only publish int 14.

igorka
Normal user
Posts: 74
Joined: 17 Jul 2022, 13:41
Location: Ukraine

Re: Mathematical operations with variables and their result.

#7 Post by igorka » 22 Jan 2023, 19:55

Ath wrote: 22 Jan 2023, 12:07 An 'endif' is missing from your 'on rules#timer=1 do' rule, not sure where it should go, but that is a very likely cause for erratic code behavior.
Finding such errors is much easier when using systematic indenting of your code.
You are absolutely right, missed 'endif' :!: Thanks for the tip ;) . I completely agree with you, the con is designed terribly, it is impossible to read it. I am writing the rules in Notepad++, the syntax has included ESPEasy 20211203. But is it possible or not to enable systematic indentation in the code so that it works automatically?
TD-er wrote: 22 Jan 2023, 12:43 Also you can easily view the current value of all variables on the sysvars page.
I find it hard to understand your code without any comments.

Indeed there is an endif missing in the last part of your rules:

Code: Select all

...
  if [int#14] > 100
    let,14,100
  else 
    if [int#14] < 0
      let,14,0
    else
      let,11,[int#11]+[int#10]
      let,11,[var#11#F]
  endif
  publish,PID_y/output,[int#14]
endon
Also you modify int 14 only if it is outside the range of 0...100, but in the else you modify int 11, without using it.
You only publish int 14.
I know about the "sysvars" function and use it, it's convenient, but the data there is static, and therefore it does not give a reliable result in all cases. Moreover, I wrote that due to the lack of “endif”, the behavior of the [int #14] variable has become unpredictable. In "sysvars" its value was displayed correctly, but in the rules it seemed to disappear, it became impossible to work with it. [int#14] and [int#11] work as intended, I posted only part of the code... Thanks for pointing this out to me ;) . In the future, I will post the full code of the program, I think it will come in handy for someone. But only after debugging his work.
This is material for a new thread...

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 29 guests