< prev index next >

src/java.base/share/classes/java/lang/Math.java

```@@ -1272,16 +1272,11 @@
* @throws ArithmeticException if the divisor {@code y} is zero
* @see #floorDiv(int, int)
* @since 1.8
*/
public static int floorMod(int x, int y) {
-        int mod = x % y;
-        // if the signs are different and modulo not zero, adjust result
-        if ((mod ^ y) < 0 && mod != 0) {
-            mod += y;
-        }
-        return mod;
+        return x - floorDiv(x, y) * y;
}

/**
* Returns the floor modulus of the {@code long} and {@code int} arguments.
* <p>
```

```@@ -1304,11 +1299,11 @@
* @see #floorDiv(long, int)
* @since 9
*/
public static int floorMod(long x, int y) {
// Result cannot overflow the range of int.
-        return (int)floorMod(x, (long)y);
+        return (int)(x - floorDiv(x, y) * y);
}

/**
* Returns the floor modulus of the {@code long} arguments.
* <p>
```

```@@ -1330,16 +1325,11 @@
* @throws ArithmeticException if the divisor {@code y} is zero
* @see #floorDiv(long, long)
* @since 1.8
*/
public static long floorMod(long x, long y) {
-        long mod = x % y;
-        // if the signs are different and modulo not zero, adjust result
-        if ((x ^ y) < 0 && mod != 0) {
-            mod += y;
-        }
-        return mod;
+        return x - floorDiv(x, y) * y;
}

/**
* Returns the absolute value of an {@code int} value.
* If the argument is not negative, the argument is returned.
```
< prev index next >