Hint: The underlined expressions / formulas match to the bold headline.
Comparisons
An expression (x0 < x1) is evaluated as comparison. When the condition is true, the value is 1, when the condition is false, the value is 0.
It is important that a comparison consists of only one character: < > =
This will not limit the possibilities, but makes them a bit more complicated.
But how can I now check if the values x0 and x1 are not equal?
We can check for greater than and less than and add both results:
(x0 > x1) + (x0 < x1) Because only one expression of both can be true at the same time, the value is 0, if x0 and x1 are equal. Otherwise the value is 1.
Not operator
But we could also check if it is equal and use the logical not operator.
Unfortunately, the Not operator ! is also not available. But here we can use the following expression:
abs((expression)-1) When we assume, the expression is either 0 or 1, then the result is a logical not.
So let us use the "is equal" example from above and plug it in:
abs( (x0 = x1) -1) If x0 equals x1, the inner expression is 1. We subtract 1 and remove the sign. The result is 0.
If x0 is not equal x1, the inner expression is 0. We subtract -1, that is -1. We remove the sign. The result is 1.
Maximum
Comparisons are helpful, if we want to use the maximum of two values x0 and x1.
(x0 > x1) * x0 will return x0, if it is greater than x1. Otherwise it will return 0.
(x1 > x0) * x1 will return x1, if it is
greater than x0. Otherwise it will return 0.
The only possible option missing is that x1 equals x0.
(x0 = x1) * x0 will return x0, if it equals x1. Otherwise it will return 0.
When we add these three expressions, we will get a formula that evaluates the maximum of two values x0 and x1:
(x0 > x1) * x0 + (x1 > x0) * x1 + (x0 = x1) * x0
Minimum
And now we know how to evaluate the minimum of two values:
(x0 < x1) * x0 + (x1 < x0) * x1 + ( x0 = x1) * x0
Clamp (Interval between Min and Max)
Here it would be nice to Clamp a value x between a given Min and Max border. Is this possible with the Math formula block without scripting?
And this is just the combination of both formulas:
(x0 > x1) * x0 + (x1 > x0) * x1 + (x0 = x1) * x0 => (Min > x) * Min + (Min = x) * Min + (x > Min) * x
(x0 < x1) * x0 + (x1 < x0) * x1 + (x0 = x1) * x0 => (Max < x) * Max + (Max = x) * Max + (x < Max) * x
(Min > x) * Min + (Min = x) * Min + (Max < x) * Max + (Max = x) * Max + (x > Min) * (x < Max) * x
Explanation:
If x is less or equal Min, use min. If x is greater or equal Max, use Max. If x is in between Min and Max, use x.
Shift Range (linear)
Sometimes you need the values in a different range:Example:
So the input range x will be shifted to an output range y withthis formula:
((x-xMin)/(xMax-xMin))*(yMax-yMin)+yMin
In the example a range of 0..2500 shall be transferred into a range from 20..100.
When combining formulas we can see an additional logic:
OR
OR
We can use the + like an Or operator.
AND
We can use the * like an And operator.
These rules apply, as long as a logical false expression evaluates to 0 and a logical true expression evaluates to >=1.
% Operator
So let us check, if a value x is even:
A value x is even, when it can be divided by 2 without a remainder. Here we can use the % operator in order to evaluate the remainder.
Example:
We want to know the remainder when calculating 5 / 3:
5 % 3 => result: 2
IsEven
In our case when the remainder is 0, the number is even.
(x0 % 2 = 0)
IsInteger
And if I want to check, if the number is odd?
Let us look, what will happen, if we use a floating point value.
4.1 % 2 = 0 evaluates to 0. This is what we expect. On the other hand, the number is odd, if there is a remainder.
(5 % 2 > 0) evaluates to 1. So is this the formula we need?
(5.1 % 2 > 0) evaluates to 1, too. A number can be even or odd, if it is an integer. This is not an integer, so we need a check for integers.
An integer value can be divided by 1 without a remainder. So our formula is:
(x0 % 1 = 0)
This evaluates to 1 for integers and 0 for floating point numbers.
Now it is time to combine the IsInteger and "Is not even" expressions:
(x0 % 2 > 0) * (x0 % 1 = 0)
x0 is odd and x0 is an integer.
A check with x0 = 5.1 evaluates to 1 * 0 = 0. 5.1 is not odd.
A check with x0 = 7 evaluates to 1 * 1 = 1. 5 is odd.
I hope, this small excursion in maths, logic and creating expressions pleased you.
Probably this will help you, solving your tasks without need to scripting.