Numeric Calculation
Description
This part allows application users to enter a mathematical expression so the system can perform a calculation.
Use
The calculation part is used by entering the requested mathematical expression in the part. The system stores the calculated result in the defined data object. This part is recommended for any formula-based math., especially when working with integers. It will correctly handle decimals but does not have built-in rounding. Two alternatives are to use a format variable part on the result, or to use a calculation to round to the required number of decimals (see Tips below).
The advanced calculation part has all the same functionality below but also
- works with "large numbers" (> 16 digits) as well as more common numbers with fewer digits
- has a parameter for rounding (such that you just specify the number of decimal places). The article for this part can be found here.
Editor Fields
Field Name |
Description/Use |
Type/Options |
Optional/Mandatory |
Data object |
This is the name of the data object in which the system will store the result of the calculation. |
Data Object |
Mandatory |
Group |
This is the path of the data object. |
Data Object |
Optional |
Calculation |
This is where the user enters the mathematical expression for the system to perform. |
Text |
Mandatory |
Round to (v7.4 on) |
When filled in, the numeric calculation will be rounded to the specified number of decimal places. When left empty, no rounding is performed on the calculation. |
Number |
Optional |
Mathematical expressions can include the following elements:
- numbers (whole and floating using ‘.’ as decimal point)
- standard operators, such as +, -, /, *, ^
- parentheses to control the precedence of the operators
- mathematical functions: sqrt, abs, ceil, floor, log, log10, max, min, mod, cos, acos, sin, asin, tan
The function max returns the maximum value of its numeric arguments, and the min function computes the minimum value. Both can be used with an unlimited number of arguments.
The function mod is equivalent to the % operator.
For trigonometric functions, angles are expressed in degrees.
Using Data Variables from the Model
Mathematical expressions can also include interpolated text , such as $(my_var).
In support of interpolation, a default value can be defined in case the variable itself has not yet been defined by the runtime logicnets. Default values should be used in most scenarios to prevent a calculation error or warning message during runtime.
Default Values
Default values are specified inside the () and after a colon as follows.
$(my_var:0) gives a result of 0 if my_var is NULL
$(my_var:1) gives a result of 1 if my_var is NULL
Sample Mathematical Expressions
sqrt(100) ==> 10
(4 – 8) * 3 ==> -12
abs(4-8)*3 ==> 12
ceil (2.6) ==> 3
floor (2.6) ==> 2
log(100) ==> 4.6051
log10(100) ==> 2
max(14, 2, 100) ==> 100
min(14, 2, 100) ==> 2
sin(90) ==> 1
asin(1) ==> 90
cos(90) ==> 6.123233995736766e-017
acos(0) ==> 90
tan(45) ==> 0.9999999999999999
Tips & Tricks
If you are using this part, but need to round a number to a given number of decimals mid-stream, it may not be appropriate to switch to the advanced calculation process node to access the rounding parameter. Instead you can use a mathematical expression to round.
ROUNDED to r places = floor ( [your-variable] * 10^r + 0.5) / 10^r
- Example: 3.14159 rounded = floor (3.14159 x 100 +0.5) / 100 = floor (314.5556) / 100 = 314 / 100 = 3.14
- Example: 8/3 rounded to 1 decimal place = floor (2.666666r * 10 + 0.5) / 10 = floor (27.166666.r ) / 10 = 27 / 10 = 2.7
In other words, we multiply then add 0.5 to round up, use floor to remove the excess digits beyond the number we want, and then divide to get back to the order of magnitude we started with.
Example
For an example form in which you can enter a mathematical expression the system will pass to the calculation part, download and import the following sample project in your workspace.