Skip to content

Compare Exact value in BigDecimalValidator min/max range checks#401

Merged
garydgregory merged 2 commits into
apache:masterfrom
sahvx655-wq:bigdecimal-compare-exact
Jun 19, 2026
Merged

Compare Exact value in BigDecimalValidator min/max range checks#401
garydgregory merged 2 commits into
apache:masterfrom
sahvx655-wq:bigdecimal-compare-exact

Conversation

@sahvx655-wq

Copy link
Copy Markdown
Contributor

Range checks on BigDecimalValidator narrow the value to a double before comparing

minValue, maxValue and isInRange ran the supplied BigDecimal through doubleValue() and compared the resulting primitive against the bound. Any value that only differs from the bound past the double mantissa is rounded onto the bound first, so the comparison is settled on a value the caller never passed. Following up on the BigIntegerValidator work I checked the same range checks here: maxValue(2^53 + 1, 2^53) returns true, because 2^53 + 1 has no exact double and rounds back to 2^53, and minValue misfires the same way for a value that rounds up onto its minimum. For a validation routine that is the failure that matters, since it lets an out-of-range value pass a bound check.

The comparison now runs against the exact BigDecimal with compareTo(BigDecimal.valueOf(bound)) for finite bounds, the same way BigIntegerValidator already compares. BigDecimal.valueOf rejects NaN and infinity, so non-finite bounds keep the doubleValue() path and the ±Infinity behaviour covered by testBigDecimalBeyondDoubleRange is unchanged. isInRange delegates to the two methods so the rule lives in one place. The added test fails on the current code and passes with the patch.

  • Read the contribution guidelines for this project.
  • Read the ASF Generative Tooling Guidance if you use Artificial Intelligence (AI).
  • I used AI to create any part of, or all of, this pull request. Which AI tool was used to create this pull request, and to what extent did it contribute?
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible, but it is a best practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that a maintainer may squash commits during the merge process.

@garydgregory

garydgregory commented Jun 19, 2026

Copy link
Copy Markdown
Member

@sahvx655-wq
Please also add tests (and possibly main changes) that cover:

  • Double.NaN
  • Double.NEGATIVE_INFINITY
  • Double.POSITIVE_INFINITY

@sahvx655-wq

Copy link
Copy Markdown
Contributor Author

Added testBigDecimalNonFiniteBounds covering all three as bounds for minValue, maxValue and isInRange. No main change was needed: BigDecimal.valueOf rejects non-finite doubles, so a NaN or ±Infinity bound keeps the doubleValue() comparison path, which already behaves sensibly. A NaN bound is never satisfied because every comparison against NaN is false; POSITIVE_INFINITY as a maximum (and NEGATIVE_INFINITY as a minimum) is an open bound any finite value meets, while the reverse pair cannot be met by a finite value.

The new test asserts each of those cases. Full suite stays green (1068 tests) and the default mvn goal builds clean.

@garydgregory garydgregory changed the title compare exact value in BigDecimalValidator min/max range checks compare Exact value in BigDecimalValidator min/max range checks Jun 19, 2026
@garydgregory garydgregory changed the title compare Exact value in BigDecimalValidator min/max range checks Compare Exact value in BigDecimalValidator min/max range checks Jun 19, 2026
@garydgregory garydgregory merged commit f08083f into apache:master Jun 19, 2026
10 checks passed
@garydgregory

Copy link
Copy Markdown
Member

Merged 🚀 Thank you @sahvx655-wq

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.

2 participants