Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<body>
<release version="3.7.2" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
<!-- FIX -->
<action dev="henrib" due-to="Amal Saidov" type="fix" issue="JEXL-478">Modulo (%) on BigInteger operands is inconsistent with other numeric types for negative operands.</action>
<action dev="henrib" due-to="Claude" type="fix" issue="JEXL-477">Interpolation strings are parsed with the engine features instead of those of their script.</action>
<action dev="henrib" due-to="Claude" type="fix" issue="JEXL-475">A jexl.options pragma modifies the engine options when evaluated with a plain context.</action>
<!-- ADD -->
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/jexl3/JexlArithmetic.java
Original file line number Diff line number Diff line change
Expand Up @@ -1387,7 +1387,7 @@ public Object mod(final Object left, final Object right) {
if (BigInteger.ZERO.equals(r)) {
throw new ArithmeticException("%");
}
final BigInteger result = l.mod(r);
final BigInteger result = l.remainder(r);
return narrowBigInteger(left, right, result);
}

Expand Down
21 changes: 21 additions & 0 deletions src/test/java/org/apache/commons/jexl3/ArithmeticTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,27 @@ void testModEdge() {
assertNullOperand(() -> jexla.divide(null, null));
}

@Test
void testModBigIntegerNegative() {
// '%' on BigInteger operands must use the truncated remainder (the sign follows the
// dividend), consistent with the long, double and BigDecimal code paths and with Java's
// '%' operator. Previously the BigInteger branch used BigInteger.mod(), which returns a
// non-negative result and throws for a negative divisor, so '-39 % 2' depended on whether
// the operands were represented as long or BigInteger.
final long[][] cases = { { -39, 2 }, { 39, -2 }, { -39, -2 }, { 39, 2 }, { -7, 3 }, { 7, -3 } };
for (final long[] c : cases) {
final long x = c[0];
final long y = c[1];
final Number longResult = (Number) jexla.mod(x, y);
final Number bigResult = (Number) jexla.mod(BigInteger.valueOf(x), BigInteger.valueOf(y));
assertEquals(x % y, longResult.longValue());
assertEquals(x % y, bigResult.longValue(),
"BigInteger '%' should match the long/Java result for " + x + " % " + y);
}
// A negative divisor must not throw for BigInteger operands (the long path does not throw).
assertEquals(1L, ((Number) jexla.mod(BigInteger.valueOf(39), BigInteger.valueOf(-2))).longValue());
}

/**
* JEXL-156.
*/
Expand Down