Skip to content

Commit c4c0711

Browse files
eamonnmcmanusGoogle Java Core Libraries
authored andcommitted
Rewrite IntMath.mod in terms of Math.floorMod.
RELNOTES=n/a PiperOrigin-RevId: 755542679
1 parent 6bbfc86 commit c4c0711

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

guava-tests/test/com/google/common/math/IntMathTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static com.google.common.math.MathTesting.POSITIVE_INTEGER_CANDIDATES;
2626
import static com.google.common.math.ReflectionFreeAssertThrows.assertThrows;
2727
import static com.google.common.math.TestPlatform.intsCanGoOutOfRange;
28+
import static com.google.common.truth.Truth.assertWithMessage;
2829
import static java.lang.Math.min;
2930
import static java.math.BigInteger.valueOf;
3031
import static java.math.RoundingMode.FLOOR;
@@ -373,12 +374,14 @@ public void testDivByZeroAlwaysFails() {
373374
public void testMod() {
374375
for (int x : ALL_INTEGER_CANDIDATES) {
375376
for (int m : POSITIVE_INTEGER_CANDIDATES) {
376-
assertEquals(valueOf(x).mod(valueOf(m)).intValue(), IntMath.mod(x, m));
377+
assertWithMessage("%s mod %s", x, m)
378+
.that(IntMath.mod(x, m))
379+
.isEqualTo(valueOf(x).mod(valueOf(m)).intValue());
377380
}
378381
}
379382
}
380383

381-
public void testModNegativeModulusFails() {
384+
public void testModNegativeModulus() {
382385
for (int x : POSITIVE_INTEGER_CANDIDATES) {
383386
for (int m : NEGATIVE_INTEGER_CANDIDATES) {
384387
assertThrows(ArithmeticException.class, () -> IntMath.mod(x, m));

guava-tests/test/com/google/common/math/LongMathTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ public void testMod() {
448448
}
449449

450450
@GwtIncompatible // TODO
451-
public void testModNegativeModulusFails() {
451+
public void testModNegativeModulus() {
452452
for (long x : ALL_LONG_CANDIDATES) {
453453
for (long m : NEGATIVE_LONG_CANDIDATES) {
454454
assertThrows(ArithmeticException.class, () -> LongMath.mod(x, m));

guava/src/com/google/common/math/IntMath.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ public static int mod(int x, int m) {
385385
if (m <= 0) {
386386
throw new ArithmeticException("Modulus " + m + " must be > 0");
387387
}
388-
int result = x % m;
389-
return (result >= 0) ? result : result + m;
388+
return Math.floorMod(x, m);
390389
}
391390

392391
/**

guava/src/com/google/common/math/LongMath.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,8 +473,7 @@ public static long mod(long x, long m) {
473473
if (m <= 0) {
474474
throw new ArithmeticException("Modulus must be positive");
475475
}
476-
long result = x % m;
477-
return (result >= 0) ? result : result + m;
476+
return Math.floorMod(x, m);
478477
}
479478

480479
/**

0 commit comments

Comments
 (0)