Modulus
Float
MOD (math/modulus/float)
The MOD node calculates the modulus (remainder) of dividing the first input by the second input. It operates on floating-point numbers.
Inputs
a
(float, default: 0): The dividend, or the number to be divided.b
(float, default: 0): The divisor, or the number to divide by.
Outputs
result
(float): The remainder after dividinga
byb
.
Configuration
This node has no configuration options.
Usage
- Connect a node providing the dividend (number to be divided) to the
a
input. - Connect a node providing the divisor (number to divide by) to the
b
input. - The
result
output will emit the remainder after dividinga
byb
.
Example
Consider a spell where you want to create a repeating color pattern based on the index of each item in a list. You can use the MOD node to get the remainder when dividing the index by the number of colors, ensuring the color index always stays within the valid range.
In this example:
- The
list/generate
node creates a list of 10 items. - The
list/map
spell runs for each item. - Inside the map spell, the
list/index
node provides the current item index. - The MOD node calculates the remainder of dividing the index by 3 (the number of colors).
- The
list/get
node uses the remainder as the index to select the color from the["red", "green", "blue"]
list. - The map spell outputs the selected color for each item.
The final output will be a list of 10 colors, repeating the red, green, blue pattern:
Best Practices
- Ensure the divisor (
b
input) is not zero, as division by zero is undefined. You may want to add ais/null
check or adefault/float
node to handle this case gracefully. - Keep in mind the MOD node works with floats. If you need integer modulus, round the inputs first or use the
math/modulus/integer
node instead.
Common Issues
- If the MOD node receives a null or non-numeric input, it will output null. Handle these cases with type checking, default values, or error handling as needed.
- Remember