Formula size limitation

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Formula size limitation

#1 Post by PandCall » 09 Jul 2023, 19:20

Hello,
I'm using a formula on the right to convert centimeters into liters.

I'm measuring the contents of a can with an HC-SR04
It returns centimeters
To find out the volume, I use this formula

Code: Select all

(%value%*(-0.58823529411765)+(23.529411764706))
But the interface cuts the formula like this

Code: Select all

%value%*(-0.58823529411765)+(23.52941176
Clearly, the formula must not exceed 40 characters.
Is it possible to change this restriction?
Regards

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

Re: Formula size limitation

#2 Post by TD-er » 09 Jul 2023, 19:51

Nope, as the settings-structure just dumps a struct, it is not that simple to extend the formula size. When changing it, you render existing settings incompatible.

But I think you can easily reduce the formula by using variables and just set those constants at boot using rules.

Code: Select all

On System#Boot Do
  Let,1,-0.58823529411765
  Let,2,23.529411764706  
Endon
The formula can then be simplified to this:
%value%*%v1%+%v2%

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

Re: Formula size limitation

#3 Post by TD-er » 09 Jul 2023, 19:56

Oh and by the way, why would you need so many decimals to begin with?
The measured value hardly ever has more than 3 or 4 significant digits, so it makes no sense to use double precision constants for this.
There are some exceptions like GPS coordinates, but for an ultrasonic sensor 3 or 4 significant digits is probably already more than the actual resolution/accuracy of the sensor.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Formula size limitation

#4 Post by PandCall » 09 Jul 2023, 20:08

Quite simply, I use a site to calculate the right and it gives me this formula.
And it usually works fine.
I'll see if I can't find a simpler formula.

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

Re: Formula size limitation

#5 Post by TD-er » 09 Jul 2023, 20:15

Just cut away the last digits.

Like:
-0.58823529411765 -> -0.58824
23.529411764706 -> 23.529

But I also gave you the fix, so no need to look for different formula.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Formula size limitation

#6 Post by PandCall » 09 Jul 2023, 20:36

No, I tested with
2 or 3 or 4 ... 7 decimals, it always gives me the same value, which is wrong.


Original Value
No Formula
No Formula
value_1.png (116.9 KiB) Viewed 1089 times

calculates the right-hand side to obtain the liters. And the liters are correct (Afterwards, I only use 2 or 3 decimal places of the result.)

Code: Select all

function x_vers_y(){
    var x = document.getElementById("nombre_x").value;
    var resultat;

    //If value is space or not a number
    if (x == "" || isNaN(x)) {
    //Not a Number
    alert("Valeur invalide !");}
    else {
    //ok
    x=parseFloat(x);
    resultat = x*(-0.58823529411765)+(23.529411764706);
    alert("Résultat : y = " + resultat);}
}
A1 and A2 information
A1 = 6 centimeters = 20 liters
A2 = 40 centimeters = 0 liters
Droite
Droite
value_2.png (56.97 KiB) Viewed 1089 times

With the short formula : (%value%*(-0.58824)+(23.529))
Formula
Formula
value_3.png (117.3 KiB) Viewed 1089 times

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

Re: Formula size limitation

#7 Post by TD-er » 09 Jul 2023, 21:31

Are you sure the value is the same?
And did you store the rules + reboot to activate it (or trigger an event like this:

Code: Select all

event,System#Boot
Without using the formula field, but just computing it in the rules, we can simply check the input and output values.
The variables 11 ... 14 are based on reduced number of decimals.

Code: Select all

on litre_uv#litre_uv do
  Let,1,-0.58823529411765
  Let,2,23.529411764706  

  let,3,%eventvalue1%*%v1%
  let,4,%v3%+%v2%
  
  Let,11,-0.58824
  Let,12,23.529

  let,13,%eventvalue1%*%v11%
  let,14,%v13%+%v12%
  
  logentry,"litre_uv:  %eventvalue1% cm. %v4% output. Less decimals: %v14%"
endon

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Formula size limitation

#8 Post by PandCall » 09 Jul 2023, 22:01

Yes, the rule works

15972183: ULTRASONIC : TaskNr: 4 Distance: 6.81 cm
15972210: EVENT: Litre_UV#Litre_UV=6.81
15972216: ACT : Let,1,-0.58823529411765
15972223: ACT : Let,2,23.529411764706
15972232: ACT : let,3,6.81*-0.588235
15972240: ACT : let,4,-4.00588+23.529412
15972248: ACT : Let,11,-0.58824
15972255: ACT : Let,12,23.529
15972263: ACT : let,13,6.81*-0.58824
15972271: ACT : let,14,-4.005914+23.529
15972280: ACT : logentry,'litre_uv: 6.81 cm. 19.523532 output. Less decimals: 19.523086'
15972285: litre_uv: 6.81 cm. 19.523532 output. Less decimals: 19.523086

But much more complex to set up.
But it works, the formula gives the result.
6.81 cm = 19.523532L
As I'm sending all the sensor values to an MQTT broker, this requires more code and complexity than applying the formula directly to %value%.

But thanks for the information.

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

Re: Formula size limitation

#9 Post by TD-er » 09 Jul 2023, 22:09

The example rules were just to show the possible differences in the computed output value between using 14 decimals or 5 decimals for your constant values.

Not intended to use it this elaborate in production use.

Simply setting the constant values at system#boot isn't that elaborate, is it?

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

Re: Formula size limitation

#10 Post by Ath » 09 Jul 2023, 22:12

PandCall wrote: 09 Jul 2023, 19:20

Code: Select all

(%value%*(-0.58823529411765)+(23.529411764706))
The braces are a bit out if wack here, I guess, can you try this in the formula field:

Code: Select all

(%value%*-0.588235)+23.529412
or with the constants set at 'on system#boot do':

Code: Select all

(%value%*%v1%)+%v2%

Viewing your log put you can see that the max. number of decimals applied during calculation is 6, and as your configured number of decimals for the result is 2, where the difference in calculated result only appears from the 4th decimal, so that doesn't change the shown result (the constants are rounded).
/Ton (PayPal.me)

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Formula size limitation

#11 Post by PandCall » 09 Jul 2023, 22:20

Yes, I understood that.
And that precision to 14 decimal places is not necessary.
I had no doubt that on 2 or 3 decimal, the result would be exploitable.
I'll have a look at why this short formula doesn't work in the formula field, maybe a syntax error.
I'll work on that.
Thank you very much for all this information.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Formula size limitation

#12 Post by PandCall » 09 Jul 2023, 22:26

Thank you Ath,
That's exactly right.
It was a syntax error in the formula that caused the problem in the short version.

With your formula
(%value%*-0.588235)+23.529412
It gives the right values in liters now.
1000 thanks to you.

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

Re: Formula size limitation

#13 Post by TD-er » 09 Jul 2023, 22:27

Just as a rule of thumb, you can look at the value with the least number of digits in the formula and using 1 or 2 digits more on the constant values will be enough resolution.

Thus if your values are like 123.4, you only have upto 4 significant digits.

But keep in mind, this is a rule of thumb, there are for sure exceptions to this :)
However that will take a bit more time to explain as it was a complete semester of "Numerical Mathematics" at university.

PandCall
Normal user
Posts: 44
Joined: 22 Sep 2022, 20:04

Re: Formula size limitation

#14 Post by PandCall » 09 Jul 2023, 22:41

No, I'm fine with your explanations.
In fact, when I used this complete formula in Python or elsewhere, it worked fine.
Then I saw that the length of the formula field was limited, but the first question was, is it possible to exceed this limitation.
But you went beyond that, helping me to understand and get workaround this formula more quickly.
Thank you again for your help and your pedagogy in helping me to understand.
Best Regards

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

Re: Formula size limitation

#15 Post by TD-er » 09 Jul 2023, 23:15

You're welcome :)

Post Reply

Who is online

Users browsing this forum: No registered users and 50 guests