The documentation for num.operator %
states (emphasis mine):
Returns the remainder of the Euclidean division. The Euclidean division of two integers a
and b
yields two integers q
and r
such that a == b * q + r
and 0 <= r < b.abs()
.
...
The sign of the returned value r
is always positive.
See remainder
for the remainder of the truncating division.
Meanwhile, num.remainder
says (again, emphasis mine):
The result r
of this operation satisfies: this == (this ~/ other) * other + r
. As a consequence the remainder r
has the same sign as the divider this
.
So if you use:
void main() {
double modulo = (-1.5).remainder(30.0);
print(modulo);
}
you'll get -1.5
.
Note that both values are mathematically correct; there are two different answers that correspond to the two different ways that you can compute a negative quotient when performing integer division. You have a choice between rounding a negative quotient toward zero (also known as truncation) or toward negative infinity (flooring). remainder
corresponds to a truncating division, and %
corresponds to a flooring division.