Returns the floor modulus of the int
arguments.
The floor modulus is x - (floorDiv(x, y) * y)
,
has the same sign as the divisor y
, and
is in the range of -abs(y) < r < +abs(y)
.
The relationship between floorDiv
and floorMod
is such that:
floorDiv(x, y) * y + floorMod(x, y) == x
The difference in values between floorMod
and
the %
operator is due to the difference between
floorDiv
that returns the integer less than or equal to the quotient
and the /
operator that returns the integer closest to zero.
Examples:
floorMod
and the %
operator are the same. floorMod(4, 3) == 1
; and (4 % 3) == 1
%
operator. floorMod(+4, -3) == -2
; and (+4 % -3) == +1
floorMod(-4, +3) == +2
; and (-4 % +3) == -1
floorMod(-4, -3) == -1
; and (-4 % -3) == -1
If the signs of arguments are unknown and a positive modulus
is needed it can be computed as (floorMod(x, y) + abs(y)) % abs(y)
.
x | the dividend | |
y | the divisor |
x - (floorDiv(x, y) * y)
ArithmeticException | if the divisor y is zero |
floorDiv(int, int)
Diagram: Math