Skip to content

keep the magnitude of truncated integer digits in the Dfp string constructor - #342

Merged
aherbert merged 1 commit into
apache:masterfrom
insaf021:dfp-string-mantissa-magnitude
Oct 28, 2026
Merged

aherbert merged 1 commit into
apache:masterfrom
insaf021:dfp-string-mantissa-magnitude

Conversation

@insaf021

Copy link
Copy Markdown
Contributor

Dfp(DfpField, String) copies at most getRadixDigits() * 4 + 1 significant digits into its scratch buffer and stops reading the input once that buffer is full. If no decimal point has been seen by then, the decimal position is taken from the number of digits stored rather than the number of integer digits in the string, so every digit past the precision is lost from the exponent as well as the mantissa. With the default 20-digit field, newDfp("123456789012345678901234567890") returns 1.2345678901234568e20 instead of roughly 1.23e29, and "1234567890123456789012345678.5" gives the same e20 value because its decimal point sits past the cut-off and is never reached. The result is a finite, plausible-looking number that is off by a factor of ten per excess digit, with nothing beyond FLAG_INEXACT to signal it. It shows up when comparing toDouble() of long decimal strings against Double.parseDouble: the two agree up to 21 digits and then diverge sharply. When the buffer fills before a decimal point, the constructor now counts the remaining integer digits up to the point and uses that as the decimal position, so the value is still rounded at the precision limit but keeps its exponent; this is separate from the exponent handling changed in #341. The new test in DfpTest fails on master with x = 1.2345678901234568e20 and passes with the patch, and mvn verify on commons-math-core, commons-math-legacy-exception and commons-math-legacy-core is clean, as are the Dfp-using tests in commons-math-legacy.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.71429% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 87.22%. Comparing base (f554608) to head (278814e).
⚠️ Report is 396 commits behind head on master.

Files with missing lines Patch % Lines
.../org/apache/commons/math4/legacy/core/dfp/Dfp.java 85.71% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master     #342      +/-   ##
============================================
+ Coverage     86.54%   87.22%   +0.67%     
+ Complexity     9787       91    -9696     
============================================
  Files           532      499      -33     
  Lines         35516    33533    -1983     
  Branches       6194     5853     -341     
============================================
- Hits          30738    29249    -1489     
+ Misses         3518     3164     -354     
+ Partials       1260     1120     -140     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@aherbert
aherbert merged commit 614c23d into apache:master Oct 28, 2026
12 checks passed
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