Kotlin negative modulo returns negative value
In version kotlin 1.1 mod
is deprecated because it was calculating the remainder of the division, which can be negative. Thats why they changed to rem
, more similar name to its functionality. If you want a modulo function in Kotlin, and the majority of languages, you can use this:
r = (a).rem(n)
if (r < 0) r += n
The best answer is Math.floorMod()
as Paul Lammertsma mentioned in a comment, since it is the most mathematically correct: It always returns a value between zero (inclusive) and the divisor (exclusive), and does so regardless of whether either or both arguments are negative.
It's even invariant across flipping the signs of all the inputs and output:
Math.floorMod(11, 9) => 2
Math.floorMod(-11, -9) => -2
Math.floorMod(-11, 9) => 7
Math.floorMod(11, -9) => -7
Math.floorMod
is available since JDK 1.8.
In Kotlin 1.5 the behavior changed and (-11).mod(9) == 7
as OP expected. In general,the result of mod
will have the same sign as the divisor.
Note that operator %
is still rem
, so (-11) % 9 == -2
.
Source: https://blog.jetbrains.com/kotlin/2021/04/kotlin-1-5-0-rc-released/