Skip to content

Use plain notation for short multipleOf divisors in error messages - #1329

Merged
stevehu merged 6 commits into
networknt:masterfrom
PHJ2000:fix-1328-master
Oct 19, 2026
Merged

stevehu merged 6 commits into
networknt:masterfrom
PHJ2000:fix-1328-master

Conversation

@PHJ2000

@PHJ2000 PHJ2000 commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Refs #1328. Corresponding 2.x PR: #1330.

Display multipleOf divisors in plain notation when the result fits within 32 characters or is no longer than BigDecimal.toString(). Otherwise, retain that representation without truncation or rounding. Check the length before expansion to avoid excessive padding for extreme scales.

Apply the same formatting to custom getDivisor() results, preserving trailing zeros such as 3.0 and 100.00. Ignore zero divisors returned by overrides, regardless of scale, matching the default conversion.

Update Javadoc and CHANGELOG, and add regression coverage for multi-digit formatting boundaries, large integers ending in zero, custom conversions, and zero divisors across ordinary and extreme scales.

Validation: mvn -B -Dtest=MultipleOfValidatorTest test on JDK 17 — 179 tests passed.

@stevehu

stevehu commented Oct 15, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR. A few findings from review. Tests pass locally (159 on JDK 17).

  1. The 32-character cap doesn't stop large integers ending in 0 from printing in scientific notation (MultipleOfValidator.formatDivisorMessage). The cap measures the whole plain string, not the zeros that plain expansion adds. For example, divisor 123456789012345678901234567890000 (33 digits) prints as 1.2345678901234567890123456789E+32, which is 34 characters versus 33 in plain form. Meanwhile the 34-digit 1234567890123456789012345678901234 prints plain. So whether a large integer shows as …E+n depends only on whether it ends in 0, which is the multipleOf: divisor shown as 1E+2 in messages; 2.x throws on a divisor that overflows double #1328 symptom above 32 digits. Suggestion: cap only the padding zeros (-scale when scale < 0, scale - precision when scale >= precision), or use plain whenever it is no longer than max(32, toString().length()).

  2. The zero-divisor cases in overriddenDivisorMessage pass only because of the chosen dividend. With an override returning 0, validating 1 throws ArithmeticException: BigInteger divide by zero. With 0.00, both 1 and 1.5 throw. The test works only because the dividend changed to 0.00000015, which reaches the early return false in isMultipleOf. Either drop the zero cases, or have the validator ignore zero divisors from overrides (as the default getDivisor already does) and assert that behavior.

  3. divisorMessageBoundsPlainNotation doesn't test the precision term of the length formula. The expected string is built with the same toPlainString()/toString() calls the implementation uses, and every boundary case has a one-digit coefficient. A formula that ignored precision would still pass. Adding multi-digit cases at the cap with literal expected strings would cover it, e.g. 1.5E+30 (32 chars, plain) and 1.5E+31 (33 chars, scientific).

  4. Zero is special-cased only for negative scales. An override returning 0E+100 shows 0, but 0E-100 shows 0E-100, and a zero with scale 30 shows a 32-character 0.000…0. If zero divisors stay supported, one rule for zero would make this consistent.

The same points apply to #1330.

@stevehu

stevehu commented Oct 16, 2026

Copy link
Copy Markdown
Contributor

Thanks for the update. I re-reviewed ac95748.

Fixed: point 3. divisorMessageBoundsPlainNotation now uses literal expected strings and multi-digit coefficients. To check it, I dropped the precision term from the length formula: 1.5e32 and -1.5e31 failed, so the precision term is now tested. MultipleOfValidatorTest passes locally (167 tests on JDK 17).

Still open (the main code hasn't changed):

  1. Large integers ending in 0 still print in scientific notation. 123456789012345678901234567890000 still gives must be multiple of 1.2345678901234567890123456789E+32.
  2. The zero-divisor cases in overriddenDivisorMessage still pass only because the dividend is 0.00000015. With an override returning 0 or 0.00, validating 1 still throws ArithmeticException: BigInteger divide by zero.
  3. Zero divisors still print as 0 or 0E-100 depending on the sign of the scale. Resolving point 2 by ignoring zero divisors would remove this too.

Please address these or reply on whether you plan to. If you disagree with any of them, a short note is fine.

@PHJ2000

PHJ2000 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the re-review. Points 1, 2, and 4 are now addressed:

  • For point 1, I used your suggested rule: plain notation when its length is at most max(32, toString().length()). Regression tests cover the reported 33-digit divisor and cases where plain notation is shorter, equal, or longer.
  • For points 2 and 4, zero divisors returned by overrides are now ignored, regardless of scale. Tests include the ordinary dividends that previously threw, as well as extreme zero scales.

The literal, multi-digit boundary tests for point 3 remain in place. Javadoc and CHANGELOG now describe the final behavior.

All 180 MultipleOfValidatorTest cases pass on JDK 17.

@stevehu

stevehu commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for this. The new formatting looks correct. I fuzzed the plain-length formula in formatDivisorMessage against toPlainString().length() on 2M random BigDecimals (signed, scales -80..80, up to 120-bit unscaled values) and found no mismatches. Two small suggestions:

1. Avoid converting to a string twice when toString() is already plain (MultipleOfValidator.formatDivisorMessage)

When the plain length is over 32 and toString() has no exponent (for example, a long integral divisor from a BigIntegerNode), length <= text.length() is true and toPlainString() repeats the BigInteger-to-decimal conversion. toString() is cached; toPlainString() isn't. Suggested change:

String text = value.toString();
if (length > text.length()) {
    return text;
}
return text.indexOf('E') < 0 ? text : value.toPlainString();

2. Simplify inheritedDivisorUsesPlainNotation

The delegate = false case is an empty anonymous subclass. It behaves exactly like the default validator, which exactDivisorMessage ("100, 3, must be multiple of 100") already covers. I'd drop the boolean parameter and keep only the override that calls super.getDivisor(node).

The same two points apply to #1330.

@PHJ2000

PHJ2000 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for checking the formatting so thoroughly. Both suggestions are now addressed:

  • Reuse toString() when it already contains plain notation, avoiding the second decimal-string conversion.
  • Simplify inheritedDivisorUsesPlainNotation to keep only the override that delegates to super.getDivisor(node).

All 179 MultipleOfValidatorTest cases and the full suite pass on JDK 17.

@stevehu stevehu 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.

Thanks for the quick turnaround. I re-reviewed a218037: both suggestions are in. The plain-notation reuse avoids the second conversion without changing output, and inheritedDivisorUsesPlainNotation now tests only the delegating override. MultipleOfValidatorTest passes locally (179 tests on JDK 21) and CI is green.

I'll follow up separately with two small master-only consistency fixes, so no changes needed here:

  • document in the getDivisor/getDividend Javadoc that non-finite nodes return null (as #1330 now does on 2.x);
  • port the failing-override case of convertedNonFiniteInputDoesNotRoundTheDivisor from #1330.

Approving and merging.

@stevehu
stevehu merged commit 1c6762d into networknt:master Oct 19, 2026
3 checks passed
stevehu added a commit that referenced this pull request Oct 19, 2026
Document that getDivisor and getDividend return null for non-finite
floating-point nodes, and add the failing-override case to
convertedNonFiniteInputDoesNotRoundTheDivisor. Follow-up to #1329.

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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