src/java.base/share/classes/java/lang/Math.java
changeset 54492 9d0ae9508d53
parent 53041 f15af1e2c683
child 54750 1851a532ddfe
--- a/src/java.base/share/classes/java/lang/Math.java	Wed Apr 10 05:15:50 2019 -0700
+++ b/src/java.base/share/classes/java/lang/Math.java	Wed Apr 10 20:03:07 2019 +0200
@@ -1274,7 +1274,12 @@
      * @since 1.8
      */
     public static int floorMod(int x, int y) {
-        return x - floorDiv(x, y) * 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;
     }
 
     /**
@@ -1301,7 +1306,7 @@
      */
     public static int floorMod(long x, int y) {
         // Result cannot overflow the range of int.
-        return (int)(x - floorDiv(x, y) * y);
+        return (int)floorMod(x, (long)y);
     }
 
     /**
@@ -1327,7 +1332,12 @@
      * @since 1.8
      */
     public static long floorMod(long x, long y) {
-        return x - floorDiv(x, y) * 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;
     }
 
     /**