
▲ 0 r/Kotlin
🧑💻 Kotlin and integer divisions
In elementary school, I learned Euclidean division:
7 ÷ 3 = 2, remainder1-7 ÷ 3 = -3, remainder2
The remainder is always positive and smaller than the divisor.
So when I started using Kotlin, I expected integer division to behave the same way.
But:
println(-7 / 3) // -2
println(-7 % 3) // -1
Kotlin follows truncating division inherited from the JVM, not Euclidean division.
That means:
a = (a / b) * b + (a % b)
still holds, but the remainder can be negative.
This surprised me because Euclidean division is usually what people first learn in mathematics.
We're currently exploring this topic while working on Kotools Types, especially around safer and more predictable integer APIs for Kotlin Multiplatform.
I’m curious:
- Did this behavior surprise you too?
- Do you prefer truncating division or Euclidean division in programming languages?
- Should Kotlin expose Euclidean operators in the standard library?
u/lvmvrquxl — 5 days ago