Repository navigation
Conversation
BigDecimal#div(value, digits) marks a nonzero remainder by bumping
frac[idx], the word after the requested digits. idx was roomof(digits,
BASE_FIG), which is only right when frac[0] has 9 digits. When it has
fewer, frac[idx] still holds requested digits, so the remainder was lost
and the result was treated as exact:
BigDecimal.mode(BigDecimal::ROUND_MODE, :up)
BigDecimal(6).div(BigDecimal('0.66'), 27) # => 0.909090909090909090909090909e1 (should end in 910)
Count the leading zeros of frac[0] when picking idx.
This branch has not been deployed
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.
div(value, digits)andquo(value, digits)can drop a nonzero remainder, so the result is rounded as if it were exact:The same calls with 26 or 28 digits are right.
Cause
After
VpDivd,BigDecimal_div2marks a nonzero remainder by bumpingfrac[idx], which should be the first word after the requested digits. It usesidx = roomof(ix, BASE_FIG), which is only true whenfrac[0]holds 9 digits. For 9.0909... the words are000000009 090909090 ..., so 27 digits end insidefrac[3], not before it.frac[3]is nonzero, nothing is bumped, and the digits after the cut are all zero, soVpLeftRoundsees an exact value.Fix
Count the leading zeros of
frac[0]and useroomof(ix + leading_zeros, BASE_FIG).idxis still insidecv(it holdsix + 1 + 2*BASE_FIG - 1digits, andleading_zeros <= 8).Testing
test_div_rounding_with_small_remainder(:up,:half_down,:half_even) fail on master and pass with this change.bundle exec rake test: 271 tests, 0 failures.div/quocalls (all 7 rounding modes, 1 to 60 digits, values from 1e-40 to 1e40) against Python'sdecimalwith the same precision and rounding. Master: 62 wrong results (28:up, 16:floor, 10:ceiling, 6:half_down, 2:half_even). This branch: 0. Division by zero was left out (it returns Infinity by design).