Modulo
This node calculates the remainder of a division operation.
The Modulo node performs the modulo operation (%).
The Modulo node returns the remainder left over when First Number is divided by Second Number. It is essential for cyclic behaviors, checking for even/odd numbers, and wrapping values.
Numbers
| Data | Description |
|---|
| First Number | The dividend (the number being divided). Default: 0. |
| Second Number | The divisor (the number to divide by). Default: 0. Cannot be 0. |
Actions
| Signal | Description |
|---|
| Do | Triggers the calculation. The result is updated only when this signal is received. |
Outputs
Data
| Data | Description |
|---|
| Result | The remainder of the division (First % Second). |
Signals
| Signal | Description |
|---|
| Done | Triggered when the calculation is successfully completed. |
Usage
Example Use Cases
- Even/Odd Check:
Number % 2. If result is 0, it's even. If 1, it's odd.
- Cyclic Index: Looping through an array repeatedly.
Current Step % Array Length = Valid Index (0 to Length-1).
- Time: Getting seconds from total milliseconds.
Total Milliseconds % 1000 = Remaining milliseconds.
- Pattern Generation: Creating repeating patterns (e.g., every 3rd item does something different).
Detailed Behavior
- Division by Zero:
- If
Second Number is 0, the node logs "Modulo by zero is not allowed" and aborts.
- Validation:
- Inputs must be valid numbers within ±1,000,000,000,000.
- Calculation:
- Uses the JS
% operator.
- Note: In JS, the sign of the result matches the dividend (
First Number). -5 % 2 is -1.
Troubleshooting
- Negative Results: If
First Number is negative, the result will be negative. If you need a positive modulo (canonical modulus), you may need additional logic (((a % n) + n) % n).