Modulo and Remainder
A brief analysis of these two operations.
For integers $a$ and $b$, the modulo or remainder operation proceeds in two steps:
- Compute the integer quotient:
c=a/b - 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:
| |