In Java, Math.floorMod(x, y) returns the modulus of x and y with the result taking the sign of the divisor. With a positive y the answer is always between 0 and y - 1, even when x is negative: Math.floorMod(-7, 3) is 2, while -7 % 3 is -1.
That is exactly what you want for anything that wraps around: an index into a circular array, the hour on a clock, the day of the week, or the bucket of a hash code that may be negative. It pairs with floorDiv.
A static method of the java.lang.Math class, added in Java 8 (JDK 1.8). Call it on the class name, as in Math.floorMod(...). No import is needed, because classes in java.lang are always visible.
Syntax
Pass the dividend, then the divisor. The usual pattern is floorMod(position, length).
Java
int a = Math.floorMod(7, 3); // 1
int b = Math.floorMod(-7, 3); // 2 (plain -7 % 3 is -1)
int c = Math.floorMod(7, -3); // -2
int next = Math.floorMod(index + step, names.length); // wrap around
Call
What it does
Math.floorMod(-1, 5)
One step back from the first slot: returns 4 (the last slot).
Math.floorMod(hour + shift, 24)
Wrap an hour onto 0..23, also for negative shifts.
Math.floorMod(hash, buckets)
A bucket number that is never negative.
Math.floorMod(7, -3)
Negative divisor, so the result is negative: -2.
floorMod(int, int) and floorMod(long, long) exist since Java 8. The (long, int) overload was added in Java 9 and returns an int, because the result always fits; on Java 8 the divisor is widened and the result is a long.
Library declarations
Java
// java.lang.Math
public static int floorMod(int x, int y)
public static int floorMod(long x, int y)
public static long floorMod(long x, long y)
Parameters
Parameter
Description
x
The dividend (an int or a long).
y
The divisor (an int or a long). Must not be zero.
Return Value
Return Value
Description
int / long
x - floorDiv(x, y) * y: zero, or a value with the same sign as y and a smaller size. For y > 0 the range is 0 to y - 1. The (long, int) overload returns an int (Java 9+).
Throws ArithmeticException
When y is zero. The message is / by zero.
Compare with Similar Methods
Remainder tools and their sign rules:
Method
How it differs
x % y
Sign of the dividend; can be negative for a positive divisor. Works on double too.
A double remainder that can be negative even for positive inputs.
Java Examples
Example 1 — Move Forward and Backward Through the Week
Seven day names live in an array. Moving back past Monday must wrap to Sunday, which the % operator cannot do.
Java
public class MathFloorModWeekdays {
public static void main(String[] args) {
String[] days = {"Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"};
int tuesday = 1;
int back3 = Math.floorMod(tuesday - 3, days.length);
System.out.println("3 days before Tue: " + days[back3]
+ " (plain % gives " + ((tuesday - 3) % days.length) + ")");
int friday = 4;
System.out.println("10 days after Fri: " + days[Math.floorMod(friday + 10, days.length)]);
int monday = 0;
System.out.println("10 days before Mon: " + days[Math.floorMod(monday - 10, days.length)]);
}
}
Output
3 days before Tue: Sat (plain % gives -2)
10 days after Fri: Mon
10 days before Mon: Fri
How It Works
Negative position. Tuesday is index 1, and 1 - 3 is -2. Used directly, % would give -2 as an index and crash with ArrayIndexOutOfBoundsException; floorMod(-2, 7) is 5, which is Sat.
Past the end. Friday is index 4 and 4 + 10 is 14, exactly two full weeks, so the remainder is 0 and the day is Mon.
Far back. 0 - 10 is -10, and adding two weeks (14) gives 4, so ten days before Mon is the day at index 4, Fri.
Example 2 — A 24-Hour Clock That Never Shows a Negative Hour
Start at 2 o'clock and shift by hours in either direction.
Java
public class MathFloorModClock {
public static void main(String[] args) {
int start = 2;
int[] shifts = {5, -5, 30, -30};
for (int shift : shifts) {
int raw = start + shift;
System.out.println("shift " + shift + ": " + Math.floorMod(raw, 24)
+ " o'clock (plain % gives " + raw % 24 + ")");
}
}
}
Forward shifts agree. 2 + 5 is 7 and 2 + 30 is 32, which is one day and 8 hours; floorMod and % both give 7 and 8 because the dividends are positive.
Backward shifts differ. 2 - 5 is -3. The operator reports -3, an impossible hour, but floorMod(-3, 24) is 21: three hours before midnight.
More than a day back. 2 - 30 is -28; adding two days (48) gives 20, which is the answer floorMod returns, while % returns -4.
Example 3 — Sign Rules, Hash Buckets, and the Zero Divisor
Try negative divisors, map a negative hash code to a bucket, and trigger the error.
Java
public class MathFloorModSigns {
public static void main(String[] args) {
System.out.println("floorMod(-7, 3) = " + Math.floorMod(-7, 3));
System.out.println("floorMod(7, -3) = " + Math.floorMod(7, -3));
System.out.println("floorMod(-7, -3) = " + Math.floorMod(-7, -3));
int hash = -17;
System.out.println("bucket for hash -17 of 5: " + Math.floorMod(hash, 5));
long big = Math.floorMod(-10_000_000_000L, 3);
System.out.println("long dividend: " + big);
try {
Math.floorMod(7, 0);
} catch (ArithmeticException e) {
System.out.println("Error: " + e.getMessage());
}
}
}
Output
floorMod(-7, 3) = 2
floorMod(7, -3) = -2
floorMod(-7, -3) = -1
bucket for hash -17 of 5: 3
long dividend: 2
Error: / by zero
How It Works
The divisor decides the sign. -7 and 3 give 2 (positive, like 3), 7 and -3 give -2 (negative, like -3), and when both are negative the result -1 stays negative.
A safe bucket. The hash -17 is negative, but floorMod(-17, 5) is 3, a valid slot between 0 and 4 (whereas -17 % 5 would be -2).
A long and a zero. -10000000000 is two more than a multiple of 3, so the remainder is 2 (an int on Java 9+, widened into the long variable); a zero divisor throws ArithmeticException.
Frequently Asked Questions
It returns the remainder of an integer division with the sign of the divisor. For a positive divisor the result is always between 0 and divisor - 1, so Math.floorMod(-7, 3) is 2.
The % operator takes the sign of the dividend, so -7 % 3 is -1. floorMod takes the sign of the divisor, so floorMod(-7, 3) is 2. They agree when both numbers have the same sign.
Use Math.floorMod(index + step, array.length). It works for positive steps, negative steps, and any size of step, and the result is always a valid index.
floorMod(int, int) and floorMod(long, long) were added in Java 8. The floorMod(long, int) overload was added in Java 9 and returns an int.
Math.floorMod throws ArithmeticException with the message / by zero, just like the % operator does for integers.