//===========================================================================
// Calculate the modulus/remainder of (dividend) divided by (divisor).
// Examples: 18 mod 5 = 3. 15 mod 5 = 0. -8 mod 5 = 2.
//
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
//===========================================================================
// Calculate the modulus/remainder of (dividend) divided by (divisor).
// Examples: 13.000 mod 2.500 = 0.500. -6.000 mod 2.500 = 1.500.
//
function ModuloReal takes real dividend, real divisor returns real
local real modulus = dividend - I2R(R2I(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