[FORMULA] Formula
Function block FORMULA calculates the results of a mathematical formula from input values, operators, and parentheses.
FORMULA calculates a mathematical formula that is too complicated to calculate using base functions.
Values for the mathematical formula are provided to the function block as real input values.
The result of the formula is supplied to the function block outputs as real or boolean values. The assembled formula is provided as a string at the outputs of the function block. The function block configuration can thus be checked and faults corrected.
A formula is configured from the input values [In1]...[In10], parentheses [Prths1]...[Prths9] and operators [Op1]...[Op9]. The following values must be configured at the inputs to calculate 1.0 + 2.0:
Input | Value | Interpreted as |
|---|---|---|
In1 | 1.0 | 1.0 |
Prths1 | 3 | No parenthesis |
Op1 | 2 | + |
In2 | 2.0 | 2.0 |
Prths2 | 3 | No parenthesis |
Op2 | 1 | No operator |
No parentheses are required in this example and that is why inputs [Prths1] and [Prths2] assigned value 3: "No parenthesis". The evaluation of the inputs stops with the first operator [Op1]...[Op9] with value 1 "No operator", is achieved: [Op2] = 1. The following operators, configured for the function block, are not considered.
Arithmetic and logical operations
FORMULA can calculate formulas with arithmetic operations.
FORMULA can also perform logical operations. Binary operators can also be used even though the input values are of type Real.
Input values equal to 0.0 are interpreted as 0 (False) and input values not equal to 0.0 as 1 (True). 0 (False) as a result of a logical operation is further calculated as real value 0.0. 1 (True) as a result of a logical operation is further calculated as real value 1.0.
Select operators
The values on inputs [Op1]...[Op9] determine the operators and type of operations:
Operator | Value |
|---|---|
None 1 | 1 |
+ | 2 |
- | 3 |
* | 4 |
/ | 5 |
AND | 6 |
OR | 7 |
> | 8 |
>= | 9 |
== | 10 |
<= | 11 |
< | 12 |
<> | 13 |
^ | 14 |
1 Processing stops here.
Operator priority
The priority of the operators determines their ranking and with it the processing sequence. The operator priorities in descending order are:
Description | Operator | Priority |
|---|---|---|
Power function | ^ | 1 |
Multiplication Division | * / | 2 |
Addition Subtraction | + - | 3 |
Greater than or equal to Greater than Equal to. Less than or equal to Less than Not equal to. | >= > == <= < <> | 4 |
AND operator | AND | 5 |
OR operator | OR | 6 |
Priority 1 is the highest priority and is performed first. If no parentheses are used, the operators with the highest priority operators are calculated first. Followed by the operators with the second highest priority. Operators of the same priority are calculated from left to right in the formula (from input [Op1] to input [Op9]).
Change the processing sequence with parentheses
Operator ranking determines the processing sequence of the operations. Parentheses can be set at inputs [Prths1]...[Prths9] if a different sequence of the operations is required. The values on the inputs determine the type of parentheses:
Parenthesis | Value |
|---|---|
(( | 1 |
( | 2 |
None | 3 |
) | 4 |
)) | 5 |
The number of opened parentheses must match the number of closed parentheses. If not, an automatic correction is performed and missing parentheses are closed.
Input | Description | Data type | Default value |
|---|---|---|---|
In1…In10 | Input 1…Input 10 | Real | 0.0 |
Prths1…Prths9 | Parentheses 1…Parentheses 9 | Integer | 3 1…5 |
1: (( | |||
Op1…Op9 | Operator 1…Operator 9 | Integer | 1: No operator 1…14 |
1: No operator | |||
Output | Description | Data type | Default value |
|---|---|---|---|
OutReal | Output real | Real | 0.0 |
OutBool | Output boolean | Boolean | 0 |
Formula | Formula | String | '' 0…90 characters |
Application example: Arithmetic operations
Assign the following values at the inputs to calculate formula [Out] = (1.0 + 2.0) * 3.0.
Input | Value | Interpreted as |
|---|---|---|
In1 | 1.0 | 1.0 |
Prths1 | 2 | ( |
Op1 | 2 | + |
In2 | 2.0 | 2.0 |
Prths2 | 4 | ) |
Op2 | 4 | * |
In3 | 3.0 | 3.0 |
Prths3 | 3 | No parenthesis |
Op3 | 1 | No operator |
This results in the following on the outputs:
Output | Value |
|---|---|
OutReal | 9.0 |
OutBool | 1 |
Formula | (ln1+In2)*In3 |
Application example: Logical operations
Set the following values at the inputs to calculate formula [Out] = -1.8 AND 2.7.
Input | Value | Interpreted as |
|---|---|---|
In1 | -1.8 | -1.8 |
Prths1 | 3 | No parenthesis |
Op1 | 6 | AND |
In2 | 2.7 | 2.7 |
Prths2 | 3 | No parentheses |
Op2 | 1 | No operator |
This results in the following on the outputs:
Output | Value |
|---|---|
OutReal | 1.0 |
OutBool | 1 |
Formula | In1&In2 |