integerdivision
Integer division is the operation of dividing two integers to produce an integer quotient, discarding any fractional part. In mathematics, the exact quotient is a real number, but many programming languages define an integer division operation that yields a whole-number result. The concept is often paired with a remainder, so that a = b q + r for integers a and b, where q is the quotient and r is the remainder.
There are two common conventions for integer division in programming languages. Truncation toward zero computes q
- 7 div 3 = 2 with remainder 1 under both conventions.
- -7 div 3: trunc toward zero gives -2 with remainder -1; floor division gives -3 with remainder
- 7 div -3: trunc toward zero gives -2 with remainder 1; floor division gives -3 with remainder
Applications of integer division include loop control, indexing, hashing, and algorithms that require discrete steps or