Repository navigation
round instead of overrunning the mantissa in the Dfp long constructor #359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
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
12345678901234567Lhas 17 digits and is reproduced as exactly the same String, yet1234567890123456789Lhas 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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No digit missed. The
12345678901234567Lline was not asserting the string form, that was theassertEqualsmessage argument, which reads like an expectation. My fault for writing it that way. It actually gives1.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, so1.23456789012346e18is correct and1.234567890123457e18is 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
99999999999999999Lcarry.One thing that fell out of this: at 18 digits the two constructors disagree.
123456789012345678Lgives1.2345678901235e17from the long path, which is nearest, but1.2345678901234e17from the string path. The string constructor keeps only the first dropped decimal digit and passesdigit * 1000toround(), so...5678looks 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Raised #362 for it. The parser keeps a single decimal digit past the mantissa and hands
round()digit * 1000, so a remainder of 5678 arrives as an exact 5000 and everything below that first digit never reaches the tie test at all; that also meansFLAG_INEXACTwas being missed. It now keeps a full radix digit of remainder plus a sticky bit for whatever is dropped below it. Cross-checked againstBigDecimalhalf-even over 80k generated values across 7 field sizes: 7665 disagreed before, none after.