Modulus
Integer
MOD (math/modulus/integer)
The MOD node calculates the modulus (remainder) of dividing two integers. It returns the remainder that results from dividing the first input integer by the second input integer.
Inputs
a
(integer, default: 0): The dividend, or the number to be divided.b
(integer, default: 0): The divisor, or the number to divide by.
Outputs
result
(integer): The remainder after dividinga
byb
.
Configuration
This node has no configuration options.
Usage
- Connect an integer value to the
a
input to specify the dividend. - Connect an integer value to the
b
input to specify the divisor. - The node will output the remainder of dividing
a
byb
via theresult
output.
Example
Let’s say you have a spell that needs to determine if a given number is odd or even. You can use the MOD node to check if the number is divisible by 2.
- Connect the integer you want to check to the
a
input of the MOD node. - Connect the integer 2 to the
b
input of the MOD node. - If the
result
output is 0, the input number is even. If theresult
is 1, the input number is odd.
Here’s what the spell might look like:
Best Practices
- Make sure to handle the case where
b
is 0, as dividing by zero is undefined. You may want to add a check before the MOD node to handle this case gracefully. - Keep in mind that the result of the modulo operation will always have the same sign as the divisor (
b
).
Common Issues
- If the
b
input is 0, the node will throw an error as division by zero is not allowed. Always ensure thatb
is not 0 before using this node. - Be careful when using negative numbers, as the behavior of the modulo operator can be unintuitive. In Magick,
a % b
always returns a value with the same sign asb
.