Modulo and Remainder

A brief analysis of these two operations.

For integers $a$ and $b$, the modulo or remainder operation proceeds in two steps:

  1. Compute the integer quotient: c=a/b
  2. Compute the modulus or remainder: r=a-(c*b)

The difference lies in the first step.

  • The remainder operation truncates the quotient toward zero — so $3.3$ and $-3.3$ become $3$ and $-3$.
  • The modulo operation truncates the quotient toward negative infinity — so $3.3$ and $-3.3$ become $3$ and $-4$.

For example, 4/(-3) ≈ -1.33:

When computing the remainder, the quotient is truncated toward zero to $-1$. When computing the modulo, the quotient is truncated toward negative infinity to $-2$. Therefore:

1
2
4 rem (-3) = 1
4 mod (-3) = -2