Skip to main content

The difference between modulo and remainder

There are difference in the calculation of modulo and remainder:

Modulo is governed by the following equation

x % y
q = floor(x / y)
x = q * y + r

if x = 13 and y = 4
13 % 4
q = floor(13/4)
   = 3
13 = 3 * 4 + r
13 = 12 + r
r = 1

Remainder is governed by the following equation:

x.remainder(y)

x - y * (x/y).truncate  (truncate means to cut off the decimals)

if x = 13 and y = 4
r = 13- 4 * (13/4).truncate
r = 13 - 4 * 3
r = 13 - 12
r = 1

Comments