-10.floorDiv(4) returns -2, expected -3

While working my way through the Kotlin docs I stopped to browse Kotlin’s standard library, writing tiny programs to to test my understanding of standard library features. Floor division surprised me.

// Intellij IDEA 2024.1
// kotlin("jvm") version "1.9.23"
fun main() {
    println(-10.floorDiv(4)) // -2
    println(kotlin.math.floor(-10.0 / 4.0)) // -3.0
}

Both java.lang.Math.floorDiv and Python’s floor division operator // return -3 when called to floor divide -10 by 4.

I believe Kotlin should also return -3 here, rather than -2. Documentation states:

Divides this value by the other value, flooring the result to an integer that is closer to negative infinity.

I searched the Kotlin Issue Tracker but did not find an open issue for floorDiv. I can create one, but hope to confirm the problem with the community first.

This is an order of operations error I believe. The following successfully prints -3:

fun main() {
    println((-10).floorDiv(4)) // -3
}
1 Like

Yes. My mistake. Thanks!