Java Utililty Methods Integer Mod

List of utility methods to do Integer Mod

Description

The list of methods to do Integer Mod are organized into topic(s).

Method

intmod(final int dividend, final int divisor)
Java's modulus operator is disappointingly different to other languages.
assert divisor > 0 : "have not implemented negative divisors yet";
int dividend2 = dividend;
if (dividend2 < 0) {
    int makePositive = (1 + -1 * dividend2 / divisor) * divisor;
    dividend2 = dividend2 + makePositive;
return dividend2 % divisor;
intmod(final int dividend, final int divisor)
mod
return dividend % divisor;
intmod(final int n, final int N)
mod
final double co;
co = (n - N * (n / N));
return ((int) co);
intmod(int a, int b)
mod
return a % b;
intMod(int a, int b)
The result of the "modulus" operator (a mod b) is defined as the remainder of the integer division (a / b).
if (b == 0)
    return a;
if (a * b >= 0)
    return a - b * (a / b);
else
    return a - b * (a / b - 1);
intmod(int a, int b)
Utility which does *proper* modding, where the result is guaranteed to be positive.
return ((a % b) + b) % b;
intmod(int a, int b)
Computes the mathematical modulus of two numbers.
return ((a % b) + b) % b;
intmod(int a, int n)
"correct" mod function.
if (n < 0)
    return mod(a, -n);
if (n == 0)
    return a % n; 
int rtn = a % n;
while (rtn < 0)
    rtn += n;
return rtn;
...
intmod(int a, int n)
return the modulus in the range [0, n)
if (n <= 0)
    throw new IllegalArgumentException();
int res = a % n;
if (res < 0) {
    res += n;
return res;
intmod(int divisor, int dividend)
mod
return (((divisor % dividend) + dividend) % dividend);