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.
aherbert
reviewed
Nov 24, 2026
| @Test | ||
| public void testLongConstructorBeyondPrecision() { | ||
| // 4 radix digits (16 decimal digits) cannot hold every long | ||
| final DfpField small = new DfpField(10); |
Contributor
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.