Skip to content

math

Description

The math block performs simple mathematical operations on numeric values provided to its input. Depending on the operation parameter, it can calculate an average, sum, minimum, maximum, difference, multiplication, or division.

The block has one output. Multiple signal sources can be connected to the input, but the required number of connections depends on the selected operating mode.

The block accepts only numeric values. Boolean values, text values, and other values that cannot be read as numbers do not take part in calculations.

The block does not support group control. It is not available as a target block type in the group-broadcast block.

Configuration parameters

Parameter name Value
Operation mode (operation) Required parameter specifying the type of calculation. Allowed values are: avg, diff_smaller_than_bigger, diff_tags, div, max, min, mul, sum.
Block duplicated value (filter_duplicated_values) Required boolean value true or false. When set to true, the block does not emit another output value identical to the last emitted one. When set to false, the result is sent after every recalculation, even if it is the same as the previous one.

Meaning of the operation parameter:

  • avg: arithmetic mean of all successfully read numeric values
  • diff_smaller_than_bigger: difference between the larger and the smaller of two input values, always non-negative
  • diff_tags: difference tag1 - tag2
  • div: quotient tag1 / tag2
  • max: largest successfully read numeric value
  • min: smallest successfully read numeric value
  • mul: product of two input values
  • sum: sum of all successfully read numeric values

Input handling

The block recalculates the result after every input change. For the input where the new value has just arrived, this new value is used. For all remaining connections, their last remembered values are used.

Only numeric inputs of integer, floating-point, temperature, or percentage type are supported. Non-numeric values are ignored or cause no result to be produced, depending on the selected mode:

  • in avg, max, min, and sum modes, each non-numeric value is simply skipped
  • in diff_smaller_than_bigger and diff_tags modes, both values must be numeric; if either of them cannot be read as a number, the result is not produced
  • in mul and div modes, both values must be numeric; if either of them cannot be read as a number, the output becomes null and the block description is set to null: Failed to read numeric value.

Connection requirements:

  • for avg, max, min, and sum, at least one block must be connected to the input; more than two connections are allowed
  • for diff_smaller_than_bigger, exactly two input connections are required
  • for diff_tags, exactly two input connections are required, and they must be exactly the tag1 and tag2 lines
  • for mul, exactly two input connections are required
  • for div, exactly two input connections are required, and they must be exactly the tag1 and tag2 lines

Inputs labeled using the tag block are meaningful in diff_tags and div modes.

Input number and name Input description
tag1: First tagged operand. In diff_tags mode, the value from this input is used as the left side of the operation tag1 - tag2. In div mode, it is used as the dividend.
tag2: Second tagged operand. In diff_tags mode, the value from this input is subtracted from tag1. In div mode, it is used as the divisor.

Additional operating rules:

  • avg calculates the average only from inputs whose values can be read as numeric
  • sum adds only valid numeric values
  • max and min consider only valid numeric values
  • if there is not a single valid numeric value in avg, sum, max, or min mode, the output becomes null
  • if one of the required numeric values cannot be read in diff_smaller_than_bigger or diff_tags mode, the output becomes null
  • if the divisor is 0 in div mode, the output becomes null and the block description becomes null: Division by zero.
  • if an arithmetic overflow occurs while calculating the result in mul or div mode, the output becomes null and the block description becomes null: Multiplication overflow. or null: Division overflow.
  • runtime errors in mul and div modes do not change the block work status; after the next valid input change, the block resumes normal result calculation

Precision and result type

The block performs calculations using the internal numeric representation with precision up to 3 decimal places.

Precision rules:

  • sum, max, min, diff_smaller_than_bigger, and diff_tags return an exact result within this precision
  • mul and div truncate the result to at most 3 decimal places
  • avg divides the sum of values by the number of valid inputs using integer division on the internal representation, so the result is truncated to at most 3 decimal places; the block does not apply any additional rounding

Result type and unit:

  • the output is returned as a regular numeric value of type integer or double
  • if the result has no fractional part, the block returns an integer
  • if the result has a fractional part, the block returns a floating-point value
  • input units such as temperature or percentage are not preserved at the output

Block outputs

The block has one output.

The output carries the result of the calculation selected by the operation parameter. If the result cannot be determined according to the rules of the selected mode, the output becomes null.

Block status description

The description shown below the block contains the textual representation of the last calculated result.

Examples:

  • 25
  • 18.5
  • 0

If the last result cannot be represented as text, the description becomes Unknown value.. This happens, among other cases, when the current result is null.

An exception applies to runtime errors in mul and div modes. In that case, the description becomes null: <message>, for example:

  • null: Failed to read numeric value.
  • null: Division by zero.
  • null: Multiplication overflow.
  • null: Division overflow.

Errors returned by the block

The block can show the following error messages:

Error code Error description
Failed read operation. The operation parameter is missing in the configuration or cannot be read as text.
Invalid operation. The operation parameter has a value outside the supported list.
Missing filter_duplicated_values configuration. The filter_duplicated_values parameter is missing in the configuration.
Input disconnected. No other block is connected to the block input.
Diff requires two connections. diff_smaller_than_bigger mode was selected, but there are not exactly two connected input sources.
Mul requires two connections. mul mode was selected, but there are not exactly two connected input sources.
Tag1 and tag2 required. diff_tags or div mode was selected, but the required tag1 and tag2 lines are not both connected, or one of them is no longer available.

Usage examples

  • If operation = sum and the available input values are 2, 3.5, and 4, the output becomes 9.5.
  • If operation = avg and the valid input values are 1, 2, and 2, the output becomes 1.666.
  • If operation = max and the block receives 21, 18, and the text open, the text value is ignored and the output becomes 21.
  • If operation = diff_smaller_than_bigger and the two input values are 7 and 10, the output becomes 3.
  • If operation = diff_tags, tag1 = 24.5, and tag2 = 20, the output becomes 4.5.
  • If operation = mul and the two input values are 2.5 and 4, the output becomes 10.
  • If operation = div, tag1 = 7, and tag2 = 2, the output becomes 3.5.
  • If operation = div, tag1 = 7, and tag2 = 0, the output becomes null, and the block description changes to null: Division by zero..
  • If operation = min, but all inputs have non-numeric values, the output becomes null and the block description changes to Unknown value..