Kelvin Chung
08/04/2025, 11:54 PMAndreyVanDenHaag
08/05/2025, 8:09 AMStephan Schröder
08/05/2025, 9:47 AM-5%2
evaluates to -1
and not 1
.Stephan Schröder
08/05/2025, 10:19 AM(-5).mod(2) -> 1
Klitos Kyriacou
08/05/2025, 11:24 AMrem
(%
) and "a version that preserves the sign of the right operand" is mod
. Neither rem
nor mod
meets the stated requirement that it "guarantees a non-negative result". Having said that, I cannot think of any valid use case for such a requirement.Stephan Schröder
08/05/2025, 1:24 PMfun Int.posRemainder(other:Int): Int = (this%other).let { rem->
if(rem>=0) {
rem
} else {
rem + kotlin.math.abs(other)
}
}
Gleb Minaev
08/05/2025, 3:43 PMfun Int.posRemainder(other:Int): Int = this.mod(abs(other))
then?Kelvin Chung
08/05/2025, 5:42 PMBigInteger
has it that rem
acts like %
(preserves the sign of the left operand) and mod
guarantees a non-negative result; they don't have an operation that preserves the sign of the right operand. Which is very confusing.Klitos Kyriacou
08/06/2025, 11:12 AM