calc (Calculate expression)
The function $calc enables the use of functionalities and mathematical functions for numerical arguments as available in the context of the Calculate value resolver in the calculation field of a form.
Syntax
$calc(expression)
Parameter
Name |
Description |
expression |
Calculation expression |
Return value
The result of the given expression as a numerical value.
Examples and remarks
Example 1:
Here the square root is taken from the element 7 and multiplied by 42.
$calc( sqrt($el(
7
) *
42
)
Example 2:
In the example, we add one hour to 15 minutes. The result (75 minutes) will be indicated in unit M (minutes), as this unit is mentioned first in the expression.
$calc(15M+1H+{
2
})
Example 3:
The 'root-mean-square' of two numerical value-elements (187, 190) should be calculated.
$calc( sqrt( $el(
187
)^
2
+ $el(
190
)^
2
) )
The expression (for the Diagonal field ) is $calc( sqrt( $el(187)^2 + $el(190)^2 ))
The calculation expression does not handle Unit Conversion in any way. In this example, the unit for the Diagonal was predefined as 'cm' (Centimeters). As both input values refer to unit 'm '(Meters), the calculation assigns this unit to the result, as well. The fact this unit is technically correct for a term calculating a square root of squared meters is just a matter of coincidence and not the result of automatic algorithmic 'reasoning'. The automatic conversion of the result from 'm' into 'cm' follows the rules of Assigning numeric values with units.
►IMPORTANT◄ If input variables with different Längeneinheit are used, the calculation expression used here will give incorrect results. The following example shows how this can be remedied.
Example 4:
The requirements of the previous example should be refined to receive correct results for sets of input values with 'mixed' length units.
This can be achieved by introducing the convert function (see Calculate value), to enforce a conversion of both length and width inputs to unit m (Meters). Any other Längeneinheit with applicable Unit Conversion could be used instead for this intermediate step.
$calc( sqrt( convert( $el(
187
),m )^
2
+ convert( $el (
190
),m ) ^
2
) )
To ensure the result (length of the diagonal) is always presented in 'cm' , this unit is defined as a default in the form design.
The expression (for the Diagonal field) is: $calc( sqrt(convert( $el(187),m)^2 + convert($el(190)) ^2 ))
►IMPORTANT◄ This example is a special case, as a result in length units is derived from input values in length units. More often, input values in length units are input for calculations of area or volume (units). It is important to consider that Unit conversion in Lobster Data Platform / Orchestration does not cover conversions between units of different dimensions automatically. If the rules for Assigning numeric values with units are observed, it is still possible to handle cross-dimensional conversions properly as demonstrated in the following example.
Example 5, with conversion between units of different dimensions:
From input values for Length, Width and Height of a shipment (with individual choice for length unit) the Loading volume should be calculated by multiplication:
On the assumption that the target unit 'cubic meters' is statically determined and therefore always set for the target element, the following configuration can be used:
The expression (for the Loading volume field) is: $calc( convert( $el(187),m) * convert($el(190),m) * convert( $el(243), m))
Values for Length, Width and Height (Element IDs #187, #190 and #243) are converted to meters by convert first, and then are multiplied. Therefore the numerical result can be read as cubic meters (m³), although it is technically linked to 'm' as a unit.
When this result is assigned to a target element with a unit of Cubic meters, Unit conversion cannot support an automatic conversion from m to m³, so the numeric value is assigned directly which results in a correct combination of value and unit for Loading volume.
Conversions of this value for Loading volume to other volume units are possible in a second step. However, another numerical attribute, here: Loading volume (converted) with a direct reference to Loading volume in a calculation expression, must be introduced for this purpose. If the option Convert value (see Numeric input with unit) is checked for the unit element of Loading volume (converted), given values are automatically converted when the unit is changed:
►NOTE◄ The elements representing Loading volume (in cubic meters) as an intermediate result are defined as inactive. It could also be set as invisible. A calculation expression is only evaluated if a reference changes or, if it does not contain a reference, only once at the time the form is loaded.