(-5).rem(3) = -2 2.2 The Modulo Function ( .mod() ) Kotlin defines the Euclidean modulo $r$ such that: $$ a \equiv r \pmodb $$ and: $$ 0 \leq r < |b| $$ Sign rule: $r$ is always non-negative.
// Verification of remainder property: // a = (a/b)*b + r // -5 = (-5/3)*3 + (-2) -> -5 = (-1)*3 + (-2) = -5 ✓ kotlin mod
fun main() val dividend = -5 val divisor = 3 println("Remainder (%.rem()): $dividend.rem(divisor)") // Output: -2 println("Modulo (.mod()): $dividend.mod(divisor)") // Output: 1 kotlin mod
Manually fixing a negative remainder: