The MathUtils.euclideanModulo()
method is a function that returns the euclidean modulo of a given dividend and divisor. The euclidean modulo is a value between 0 (inclusive) and the absolute value of the divisor (exclusive).
MathUtils.euclideanModulo(dividend, divisor)
dividend
- A number representing the dividend in the equation.
divisor
- A number representing the divisor in the equation.
The MathUtils.euclideanModulo()
method returns a number representing the euclidean modulo of the dividend and divisor.
MathUtils.euclideanModulo(7, 3); //returns 1
MathUtils.euclideanModulo(-7, 3); //returns 2
MathUtils.euclideanModulo(7, -3); //returns 1
MathUtils.euclideanModulo(-7, -3); //returns 2
If either dividend
or divisor
is not a number, or if divisor
is 0, NaN
is returned.
MathUtils.euclideanModulo()
works by first calculating the regular modulo using the %
operator, and then adjusting the value to fall within the euclidean range. This is done by adding the absolute value of the divisor to the value until it is greater than or equal to 0 and less than the absolute value of the divisor.
This method can be useful in many mathematical algorithms, such as calculating the index of an array in a circular manner, or determining the position of an element in an evenly spaced grid.