Skip to content

JEXL-478: Make % (mod) on BigInteger operands use truncated remainder, consistent with other numeric types - #429

Merged
henrib merged 2 commits into
apache:masterfrom
amalsaidov:fix/mod-biginteger-remainder
Dec 2, 2026
Merged

henrib merged 2 commits into
apache:masterfrom
amalsaidov:fix/mod-biginteger-remainder

Conversation

@amalsaidov

Copy link
Copy Markdown
Contributor

The BigInteger branch of JexlArithmetic.mod(Object, Object) uses BigInteger.mod(), whereas every other branch of the same method (long, double, BigDecimal) uses the truncated remainder, where the sign follows the dividend. BigInteger.mod() instead returns a non-negative (floored) result and throws ArithmeticException for a negative divisor.

Because the branch taken depends only on the runtime representation of the operands, the result of a % expression is inconsistent across numeric types:

-39 % 2    =>  -1        -39H % 2H   =>  1
 39 % -2   =>   1         39H % -2H  =>  throws "BigInteger: modulus not positive"
-39.0 % 2.0 => -1.0      // (BigDecimal/double paths agree with long)

The same inconsistency is reachable without the H suffix whenever an integer literal exceeds the long range (it is parsed as a BigInteger).

This also makes % inconsistent with JexlArithmetic.divide, whose BigInteger branch uses BigInteger.divide() (truncating toward zero). The identity (a / b) * b + (a % b) == a therefore fails for negative BigInteger operands.

Fix

Use BigInteger.remainder() instead of BigInteger.mod() in the BigInteger branch. remainder() is the truncated remainder (sign of the dividend) and is the exact companion of BigInteger.divide(), so % now behaves identically across long, double, BigDecimal and BigInteger, and matches Java's % operator.

After the change:

-39H % 2H   =>  -1        39H % -2H  =>  1   (no longer throws)

Test

Adds ArithmeticTest.testModBigIntegerNegative, which asserts that % on BigInteger operands matches the long/Java result for negative dividends and divisors, and that a negative divisor no longer throws. The test fails before the change (expected: <-1> but was: <1>) and passes after.

The existing positive-operand test (testBigInteger: 6 % 2 == 0) is unaffected, since mod and remainder agree for non-negative operands. The full test suite (1239 tests) passes and checkstyle reports no violations.

The BigInteger branch of JexlArithmetic.mod used BigInteger.mod(), which
returns a non-negative result and throws ArithmeticException for a negative
divisor. The long, double and BigDecimal branches all use the truncated
remainder (sign follows the dividend), as does Java's % operator and
JexlArithmetic.divide (which truncates toward zero). As a result, the value
of a % expression depended on the runtime representation of its operands:

  -39 % 2   -> -1      -39H % 2H  -> 1
  39 % -2   -> 1       39H % -2H  -> throws "modulus not positive"

Switch the BigInteger branch to BigInteger.remainder() so that % is
consistent across all numeric types and with integer division. Add a
regression test covering negative dividends and divisors.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues block the change aligning BigInteger remainder behavior with other numeric types.

No open issues 🎉

What changed in this PR

Aligns BigInteger % behavior with other numeric types in JEXL by using truncated remainder.

Changes:

  • Replaces BigInteger.mod() with remainder(), supporting negative divisors and preserving the dividend’s sign.
  • Adds regression tests comparing BigInteger results with Java’s % across operand sign combinations.
File Description
src/​test/​java/​org/​apache/​commons/​jexl3/​ArithmeticTest.java Tests remainder consistency for positive and negative operands.
src/​main/​java/​org/​apache/​commons/​jexl3/​JexlArithmetic.java Uses truncated remainder for BigInteger operands.

Review effort: Balanced

@henrib henrib changed the title Make % (mod) on BigInteger operands use truncated remainder, consistent with other numeric types JEXL-478: Make % (mod) on BigInteger operands use truncated remainder, consistent with other numeric types Dec 2, 2026
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@henrib
henrib merged commit afcf2b2 into apache:master Dec 2, 2026
16 checks passed
@henrib

henrib commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Thank you for you contribution @amalsaidov 👍

@amalsaidov

Copy link
Copy Markdown
Contributor Author

Thanks for merging! Appreciate the quick review 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants