Repository navigation
Conversation
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Thanks for the more explicit tests. Please raise a PR with a fix to the String parsing.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
Reproduced on master, JDK 21:
Dfp(DfpField, long)shifts each radix digit into the mantissa withSystem.arraycopyand never checksexpagainstmant.length. AnyDfpFieldbelow 17 decimal digits has 4 radix digits (16 decimal), but a long needs up to 5, so every|x| >= 10^16overruns,Long.MAX_VALUEandLong.MIN_VALUEincluded.DfpField.newDfp(long)andDfp.newInstance(long)both land here, whereasnewDfp("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. TheLong.MIN_VALUE+1 correction goes to that digit when one was dropped. Fields of 17 or more digits take the old path unchanged.testLongConstructorBeyondPrecisioninDfpTestchecks 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:javadocon legacy-core is green.