Does anyone know why Kotlin BigDecimal div operato...
# mathematics
p
Does anyone know why Kotlin BigDecimal div operator decided to use the scaling of the nominator and behave different from the equivalent Java implementation?
Copy code
import java.math.BigDecimal

val x = BigDecimal("127.0")
val y = BigDecimal("50")

println(x / y)  // 2.5 unexpected
println(x.divide(y)) // 2.54 what I expected
Took me while to figure out why what was going on in my code and I cannot see many cases where this is the desired behaviour. Would rather have the div operator just calling the divide method.
a
It is why it is better to define context for unclear operations. As for the question, I don't know. It just usese rounding mode: https://github.com/JetBrains/kotlin/blob/6a670dc5f38fc73eb01d754d8f7c158ae0176ceb/libraries/stdlib/jvm/src/kotlin/util/BigDecimals.kt#L40
p
Thanks, still think the default Java behaviour makes more sense and I rather have an exception thrown if a rounding policy is really required than loose precision if that doesn’t have to be the case. But anyway, the workaround is to just call the Java method instead.
r
Why are you using big decimal?
set the scale on x and y and x / y will yield same result as x.divide(y)