Repository navigation
round on the whole remainder in the Dfp string constructor - #362
Merged
Merged
Conversation
The parser kept a single digit past the mantissa and rounded with round(digit * 1000). A dropped "5678" therefore looked like an exact half and ROUND_HALF_EVEN took it down, and a non-zero digit further down was lost entirely, so a tie was never broken and FLAG_INEXACT was not raised. Keep a full radix digit of remainder, track whether anything non-zero was dropped below it, and pass that to round().
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #362 +/- ##
============================================
+ Coverage 86.54% 87.22% +0.68%
+ Complexity 9787 91 -9696
============================================
Files 532 499 -33
Lines 35516 33555 -1961
Branches 6194 5862 -332
============================================
- Hits 30738 29270 -1468
+ Misses 3518 3164 -354
+ Partials 1260 1121 -139 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Follow-up to the string parsing issue found while reviewing #359.
Dfp(DfpField, String)keeps one decimal digit past the mantissa and rounds with it:round()takes its argument as a fraction of the last radix digit out of 10000, so a single decimal digit can only ever be a multiple of 1000. Two things fall out of that:5678is passed as5000, whichROUND_HALF_EVENreads as an exact half and takes down. On a 4 radix digit field,newDfp("123456789012345678")gives1.2345678901234e17where the nearest value is1.2345678901235e17.round()sees it, so a tie is never broken andFLAG_INEXACTis never raised for them.new DfpField(20).newDfp("1.0000000000000000000000001")returns1.with no flags set.The parser copies at most
mant.length * rsize + 1digits, so the information needed to decide either case was being thrown away during the copy.The fix keeps a whole radix digit of remainder instead of one decimal digit, which is exactly the precision
round()works in, and records whether a non-zero digit was dropped below that. Anything below the four digits only matters as "the remainder is strictly greater", so it is tracked as a single sticky bit and used to nudgenoff the two valuesround()tests exactly, zero and 5000. Digits dropped during the copy and digits still sitting in the parsed string are both covered.stripedis now filled with'0'up front, because the rounding digits can sit past the last position the copy and the zero padding write.Checked against
BigDecimal.round(new MathContext(keep, HALF_EVEN))over 80k generated values across 7 field sizes, biased towards 0, 5 and 9 so ties and carries come up: 7665 disagreed before, none after. A sweep of every field size from 1 to 45 digits, with integer, fractional, mixed and exponent forms from 1 digit up to past the mantissa, also agrees throughout.testStringConstructorRoundinginDfpTestpins the 16 to 19 digit bands with explicit expected strings, rounding up and down in each, both signs, exact halves going to even, a half broken by a digit inside the parsed string and by one far past it, the carry out of the mantissa, and the flags each case raises. It fails on master at the 18 digit case and passes with the fix.mvn clean verify javadoc:javadocis green on legacy-core and legacy, checkstyle reports no violations.