Skip to content

round instead of overrunning the mantissa in the Dfp long constructor - #359

Open
insaf021 wants to merge 2 commits into
apache:masterfrom
insaf021:dfp-long-constructor-round
Open

insaf021 wants to merge 2 commits into
apache:masterfrom
insaf021:dfp-long-constructor-round

Conversation

@insaf021

Copy link
Copy Markdown
Contributor

Reproduced on master, JDK 21:

new DfpField(10).newDfp(12345678901234567L)
java.lang.ArrayIndexOutOfBoundsException: arraycopy: destination index -1 out of bounds for int[4]

Dfp(DfpField, long) shifts each radix digit into the mantissa with System.arraycopy and never checks exp against mant.length. Any DfpField below 17 decimal digits has 4 radix digits (16 decimal), but a long needs up to 5, so every |x| >= 10^16 overruns, Long.MAX_VALUE and Long.MIN_VALUE included. DfpField.newDfp(long) and Dfp.newInstance(long) both land here, whereas newDfp("12345678901234567") on the same field rounds to the precision as expected.

Once the mantissa is full, the least significant digit is now shifted out and the value is rounded with it through round(), the same path the string constructor uses. The Long.MIN_VALUE +1 correction goes to that digit when one was dropped. Fields of 17 or more digits take the old path unchanged.

testLongConstructorBeyondPrecision in DfpTest checks that the long and string constructors agree on a 10-digit field. It fails on master with the exception above and passes with the fix; mvn clean verify javadoc:javadoc on legacy-core is green.

Dfp(DfpField, long) shifted every radix digit into the mantissa without
checking its length. Fields below 17 decimal digits have 4 radix digits
while a long needs up to 5, so any |x| >= 10^16 threw
ArrayIndexOutOfBoundsException. Shift the least significant digit out
once the mantissa is full and round with it, as the string constructor
does.
@Test
public void testLongConstructorBeyondPrecision() {
// 4 radix digits (16 decimal digits) cannot hold every long
final DfpField small = new DfpField(10);

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.

The source code shows decimal digits < 13 using 4 radix digits (radix=10000). So we have configured for 16 decimal digits. But the long value 12345678901234567L has 17 digits and is reproduced as exactly the same String, yet 1234567890123456789L has 19 digits and is reproduced as a 15 digit String plus exponent. So has rounding missed a digit? Should the result be "1.234567890123457e18"?

This should show behaviour with 16, 17, 18 and 19 digit strings that expect rounding up or down when the Dfp has to round.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

No digit missed. The 12345678901234567L line was not asserting the string form, that was the assertEquals message argument, which reads like an expectation. My fault for writing it that way. It actually gives 1.234567890123e16.

The precision is quantised rather than a flat 16 decimal digits. The decimal point has to land on a radix boundary, so at 19 digits the leading radix digit carries only 3 decimal digits (123) and one decimal position inside it is a leading zero. That leaves 15 significant digits, so 1.23456789012346e18 is correct and 1.234567890123457e18 is not representable on this field. 17 digits keeps 13, 18 keeps 14, 19 keeps 15. newDfp("1234567890123456789") returns the same value.

I checked each case against BigDecimal.round(new MathContext(keep, HALF_EVEN)) and the long constructor lands on the nearest representable value every time, including the carry out of the mantissa and the halfway ties.

Test rewritten with explicit expected strings for 16, 17, 18 and 19 digits, rounding down and up in each band, both signs, the two halfway cases that show the tie going to even, and the 99999999999999999L carry.

One thing that fell out of this: at 18 digits the two constructors disagree. 123456789012345678L gives 1.2345678901235e17 from the long path, which is nearest, but 1.2345678901234e17 from the string path. The string constructor keeps only the first dropped decimal digit and passes digit * 1000 to round(), so ...5678 looks like an exact tie and half-even takes it down. That is pre-existing and I have left it alone here. Happy to fix it separately if you want it.

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 more explicit tests. Please raise a PR with a fix to the String parsing.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.25%. Comparing base (f554608) to head (724bcb7).
⚠️ Report is 427 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master     #359      +/-   ##
============================================
+ Coverage     86.54%   87.25%   +0.70%     
+ Complexity     9787       91    -9696     
============================================
  Files           532      499      -33     
  Lines         35516    33550    -1966     
  Branches       6194     5859     -335     
============================================
- Hits          30738    29274    -1464     
+ Misses         3518     3160     -358     
+ Partials       1260     1116     -144     

☔ 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.

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