# ModuloInteger

Parameters
 dividend `integer` divisor `integer`
bug

The commented law doesn't hold. For example `ModuloInteger(-7, -3) == -4` while `-7 % -3 == -1`

comment

Calculate the modulus/remainder of (dividend) divided by (divisor) such that `(dividend / divisor) * divisor + ModuloInteger(dividend, divisor) == dividend` with `/` rounding towards negative infinity. Examples: 18 mod 5 = 3. 15 mod 5 = 0. -8 mod 5 = 2.

note

Use the `%`-operator as it's probably faster but also correct.

return type
`integer`
Source code
``````function ModuloInteger takes integer dividend, integer divisor returns integer
local integer modulus = dividend - (dividend / divisor) * divisor

// If the dividend was negative, the above modulus calculation will
// be negative, but within (-divisor..0).  We can add (divisor) to
// shift this result into the desired range of (0..divisor).
if (modulus < 0) then
set modulus = modulus + divisor
endif

return modulus
endfunction``````
Source
Blizzard.j
wc3modding.com
ModuloInteger