Skip to main content
Version: 1.3.0

Math

Arithmetic Data Analysis

Synopsis

Performs mathematical operations on numeric values.

Schema

- math:
field: <ident>
operation: <string>
left_operand: <string>
right_operand: <string>
precision: <numeric>
description: <text>
if: <script>
ignore_failure: <boolean>
ignore_missing: <boolean>
on_failure: <processor[]>
on_success: <processor[]>
tag: <string>

Configuration

The following fields are used to define the processor:

FieldRequiredDefaultDescription
fieldY-Target field to store the calculation result
operationY-Mathematical operation to perform
left_operandY-First operand value or field reference
right_operandN-Second operand value or field reference (required for binary operations)
precisionN0Number of decimal places for rounding operations
descriptionN-Explanatory note
ifN-Condition to run
ignore_failureNfalseContinue processing if calculation fails
ignore_missingNfalseSkip if referenced fields don't exist
on_failureN-Error handling processors
on_successN-Success handling processors
tagN-Identifier

Supported Operations

Binary Operations (two operands)

OperationAliasesDescription
add+Addition
subtract-Subtraction
multiply*Multiplication
divide/Division
modulo%Remainder after division
pow^Exponentiation (power)
min-Minimum of two values
max-Maximum of two values

Unary Operations (one operand)

OperationDescription
absAbsolute value
sqrtSquare root
ceilRound up to nearest integer
floorRound down to nearest integer
roundRound to nearest integer or decimal places (using precision)

Details

Performs mathematical operations on numeric values and stores the result in a target field. The processor supports both binary operations (requiring two operands) and unary operations (requiring a single operand).

note

Numeric values are automatically converted to appropriate types for calculation. The processor accepts both literal values and field references for operands, and can handle expressions like field1 / field2 in the operands.

The processor can extract values from existing fields, use literal numeric values, and applies appropriate error handling for mathematical edge cases like division by zero or invalid operations.

warning

Some operations have mathematical constraints and will fail in certain conditions:

  • Division or modulo by zero
  • Square root of negative numbers
  • 0 raised to the power of 0 (indeterminate)
  • Negative numbers raised to non-integer powers

Always provide appropriate error handling for operations that might encounter these edge cases.

Examples

Basic

Performing addition with literal values...

{
"metrics": {}
}
- math:
field: metrics.total
operation: add
left_operand: "100"
right_operand: "50"

calculates and stores the result:

{
"metrics": {
"total": 150
}
}

Field-Based

Calculating percentage from field values...

{
"disk": {
"used": 8589934592,
"total": 17179869184
}
}
- math:
field: disk.usage_percent
operation: multiply
left_operand: "disk.used / disk.total"
right_operand: "100"
precision: 2

computes disk usage percentage:

{
"disk": {
"used": 8589934592,
"total": 17179869184,
"usage_percent": 50
}
}

Unary

Applying unary operations...

{
"temperature": {
"celsius": -5.7
}
}
- math:
field: temperature.absolute
operation: abs
left_operand: "temperature.celsius"

calculates absolute value:

{
"temperature": {
"celsius": -5.7,
"absolute": 5.7
}
}

Precision Rounding

Rounding to specified decimal places...

{
"stats": {
"raw_value": 123.45678
}
}
- math:
field: stats.rounded
operation: round
left_operand: "stats.raw_value"
precision: 2

rounds to two decimal places:

{
"stats": {
"raw_value": 123.45678,
"rounded": 123.46
}
}

Complex

Combining operations in a pipeline...

{
"circle": {
"radius": 5
}
}
- math:
field: circle.radius_squared
operation: pow
left_operand: "circle.radius"
right_operand: "2"
- math:
field: circle.area
operation: multiply
left_operand: "circle.radius_squared"
right_operand: "3.14159"
precision: 2

calculates area in multiple steps:

{
"circle": {
"radius": 5,
"radius_squared": 25,
"area": 78.54
}
}

Error Handling

Handling potential calculation errors...

{
"stats": {
"numerator": 100,
"denominator": 0
}
}
- math:
field: stats.result
operation: divide
left_operand: "stats.numerator"
right_operand: "stats.denominator"
ignore_failure: true
on_failure:
- set:
field: stats.error
value: "Division by zero error"
- set:
field: stats.result
value: 0

continues execution:

{
"stats": {
"numerator": 100,
"denominator": 0,
"error": "Division by zero error",
"result": 0
}
}