Skip to content

Fix division rounding #330

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 1 commit into from
May 29, 2025
Merged

Fix division rounding #330

merged 1 commit into from
May 29, 2025

Conversation

tompng
Copy link
Member

@tompng tompng commented May 27, 2025

Fixes #328
Depend on #329

Rounding needs these two:

  • next digit
  • boolean value remaining digits == zero

These are some rounding rules:
ROUND_HALF_UP: next_digit>=5
ROUND_HALF_DOWN: next_digit>5 || (next_digit==5 && !remaining_zero)
ROUND_UP: next_digit>0 || !remaining_zero

Currently, BigDecimal_div2 only uses next few digits.
This pull request adds extra information about remaining_digits before rounding.

@mrkn mrkn added this to the v3.2 milestone May 29, 2025
When the intermediate calculation of `a.div(b, 4)` is `0.123000000 000000000 ...`,
Round up result may be 0.1230 or 0.1231 depend on remainder of the division.
Treat the calculation as `0.123000000 000000001` in such case to calculate rounding correctly.
@tompng tompng marked this pull request as ready for review May 29, 2025 09:16
@mrkn mrkn merged commit c216ed4 into ruby:master May 29, 2025
78 checks passed
@tompng tompng deleted the div2_round_fix branch May 29, 2025 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

BigDecimal#div doesn't round correctly
2 participants