equal
deleted
inserted
replaced
1272 * @throws ArithmeticException if the divisor {@code y} is zero |
1272 * @throws ArithmeticException if the divisor {@code y} is zero |
1273 * @see #floorDiv(int, int) |
1273 * @see #floorDiv(int, int) |
1274 * @since 1.8 |
1274 * @since 1.8 |
1275 */ |
1275 */ |
1276 public static int floorMod(int x, int y) { |
1276 public static int floorMod(int x, int y) { |
1277 return x - floorDiv(x, y) * y; |
1277 int mod = x % y; |
|
1278 // if the signs are different and modulo not zero, adjust result |
|
1279 if ((mod ^ y) < 0 && mod != 0) { |
|
1280 mod += y; |
|
1281 } |
|
1282 return mod; |
1278 } |
1283 } |
1279 |
1284 |
1280 /** |
1285 /** |
1281 * Returns the floor modulus of the {@code long} and {@code int} arguments. |
1286 * Returns the floor modulus of the {@code long} and {@code int} arguments. |
1282 * <p> |
1287 * <p> |
1299 * @see #floorDiv(long, int) |
1304 * @see #floorDiv(long, int) |
1300 * @since 9 |
1305 * @since 9 |
1301 */ |
1306 */ |
1302 public static int floorMod(long x, int y) { |
1307 public static int floorMod(long x, int y) { |
1303 // Result cannot overflow the range of int. |
1308 // Result cannot overflow the range of int. |
1304 return (int)(x - floorDiv(x, y) * y); |
1309 return (int)floorMod(x, (long)y); |
1305 } |
1310 } |
1306 |
1311 |
1307 /** |
1312 /** |
1308 * Returns the floor modulus of the {@code long} arguments. |
1313 * Returns the floor modulus of the {@code long} arguments. |
1309 * <p> |
1314 * <p> |
1325 * @throws ArithmeticException if the divisor {@code y} is zero |
1330 * @throws ArithmeticException if the divisor {@code y} is zero |
1326 * @see #floorDiv(long, long) |
1331 * @see #floorDiv(long, long) |
1327 * @since 1.8 |
1332 * @since 1.8 |
1328 */ |
1333 */ |
1329 public static long floorMod(long x, long y) { |
1334 public static long floorMod(long x, long y) { |
1330 return x - floorDiv(x, y) * y; |
1335 long mod = x % y; |
|
1336 // if the signs are different and modulo not zero, adjust result |
|
1337 if ((x ^ y) < 0 && mod != 0) { |
|
1338 mod += y; |
|
1339 } |
|
1340 return mod; |
1331 } |
1341 } |
1332 |
1342 |
1333 /** |
1343 /** |
1334 * Returns the absolute value of an {@code int} value. |
1344 * Returns the absolute value of an {@code int} value. |
1335 * If the argument is not negative, the argument is returned. |
1345 * If the argument is not negative, the argument is returned. |