Repository navigation
JEXL-478: Make % (mod) on BigInteger operands use truncated remainder, consistent with other numeric types - #429
Merged
Conversation
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.
Contributor
There was a problem hiding this comment.
🟢 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()withremainder(), 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
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Contributor
|
Thank you for you contribution @amalsaidov 👍 |
Contributor
Author
|
Thanks for merging! Appreciate the quick review 🙏 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The BigInteger branch of
JexlArithmetic.mod(Object, Object)usesBigInteger.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 throwsArithmeticExceptionfor 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:The same inconsistency is reachable without the
Hsuffix whenever an integer literal exceeds thelongrange (it is parsed as aBigInteger).This also makes
%inconsistent withJexlArithmetic.divide, whose BigInteger branch usesBigInteger.divide()(truncating toward zero). The identity(a / b) * b + (a % b) == atherefore fails for negative BigInteger operands.Fix
Use
BigInteger.remainder()instead ofBigInteger.mod()in the BigInteger branch.remainder()is the truncated remainder (sign of the dividend) and is the exact companion ofBigInteger.divide(), so%now behaves identically acrosslong,double,BigDecimalandBigInteger, and matches Java's%operator.After the change:
Test
Adds
ArithmeticTest.testModBigIntegerNegative, which asserts that%onBigIntegeroperands matches thelong/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, sincemodandremainderagree for non-negative operands. The full test suite (1239 tests) passes and checkstyle reports no violations.