Skip to main content
Version: 1.4.0

Modulo

Arithmetic

Synopsis

Calculates the remainder after division of two numeric values.

Schema

- modulo:
field: <ident>
left_operand: <string>
right_operand: <string>
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 remainder result
left_operandY-Dividend (numerator) - can be a literal value or field reference
right_operandY-Divisor (denominator) - can be a literal value or field reference
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

Details

Calculates the remainder when dividing the left operand by the right operand and stores the result in the target field. The processor uses floating-point modulo calculation to handle both integer and decimal values.

note

The modulo operation uses floating-point division, which means it can handle both integer and non-integer values. For integer division, consider using a combination of the divide and floor operations.

The processor is useful for cyclic calculations, determining if numbers are divisible evenly, and implementing various bucketing or grouping logic.

warning

Division by zero will cause the processor to fail unless ignore_failure is set to true. Always validate the right operand or provide proper error handling to avoid this error.

Examples

Basic

Calculating remainder with literal values...

{
"math": {}
}
- modulo:
field: math.remainder
left_operand: "17"
right_operand: "5"

calculates and stores the result:

{
"math": {
"remainder": 2
}
}

Divisibility

Determining if a number is divisible by another...

{
"number": 15,
"check": {
"divisor": 3
}
}
- modulo:
field: check.remainder
left_operand: "number"
right_operand: "check.divisor"
- set:
field: check.is_divisible
value: "{{#eq check.remainder 0}}true{{else}}false{{/eq}}"

and flagging divisibility status:

{
"number": 15,
"check": {
"divisor": 3,
"remainder": 0,
"is_divisible": true
}
}

Time Cycles

Mapping time-of-day to cycle position...

{
"timestamp": {
"hour": 23
}
}
- modulo:
field: timestamp.day_cycle
left_operand: "timestamp.hour"
right_operand: "12"
- set:
field: timestamp.period
value: "{{#eq timestamp.day_cycle 0}}12{{else}}{{timestamp.day_cycle}}{{/eq}}"
- set:
field: timestamp.am_pm
value: "{{#lt timestamp.hour 12}}AM{{else}}PM{{/eq}}"

for 12-hour clock formatting:

{
"timestamp": {
"hour": 23,
"day_cycle": 11,
"period": 11,
"am_pm": "PM"
}
}

Data Partitioning

Assigning requests to backend servers...

{
"request": {
"id": "74fc316a",
"hash": 1273809654,
"path": "/api/user/profile"
},
"cluster": {
"server_count": 5
}
}
- modulo:
field: request.server_index
left_operand: "request.hash"
right_operand: "cluster.server_count"

distributes load with consistent hashing:

{
"request": {
"id": "74fc316a",
"hash": 1273809654,
"path": "/api/user/profile",
"server_index": 4
},
"cluster": {
"server_count": 5
}
}