Repository navigation
Conversation
For a negative base and an odd exponent, power returned -(-x).power(y, prec). The positive value was rounded first and then negated, so :floor rounded toward zero and :ceiling away from it: BigDecimal.mode(BigDecimal::ROUND_MODE, :floor) BigDecimal(-3).power(3, 1) # => -0.2e2 (should be -0.3e2) Swap :floor and :ceiling for the inner call.
tompng
reviewed
Nov 19, 2026
Comment on lines
+191
to
+197
| if mode == BigDecimal::ROUND_FLOOR || mode == BigDecimal::ROUND_CEILING | ||
| return BigDecimal.save_rounding_mode do | ||
| BigDecimal.mode(BigDecimal::ROUND_MODE, mode == BigDecimal::ROUND_FLOOR ? BigDecimal::ROUND_CEILING : BigDecimal::ROUND_FLOOR) | ||
| -(-x).power(y, prec) | ||
| end | ||
| end | ||
| return -(-x).power(y, prec) |
Member
There was a problem hiding this comment.
Good catch, thanks! Since a negative base only works when y is an integer, this path is rarely taken. I think we can always use save_rounding_mode here. It won't be a bottleneck, and the code gets simpler:
Suggested change
| if mode == BigDecimal::ROUND_FLOOR || mode == BigDecimal::ROUND_CEILING | |
| return BigDecimal.save_rounding_mode do | |
| BigDecimal.mode(BigDecimal::ROUND_MODE, mode == BigDecimal::ROUND_FLOOR ? BigDecimal::ROUND_CEILING : BigDecimal::ROUND_FLOOR) | |
| -(-x).power(y, prec) | |
| end | |
| end | |
| return -(-x).power(y, prec) | |
| BigDecimal.save_rounding_mode do | |
| if mode == BigDecimal::ROUND_FLOOR || mode == BigDecimal::ROUND_CEILING | |
| BigDecimal.mode(BigDecimal::ROUND_MODE, mode == BigDecimal::ROUND_FLOOR ? BigDecimal::ROUND_CEILING : BigDecimal::ROUND_FLOOR) | |
| end | |
| return -(-x).power(y, prec) | |
| end |
Author
There was a problem hiding this comment.
Done in 535461d, thanks. save_rounding_mode restores the mode on the early return too (rb_protect), so this is fine.
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.
With a negative base and an odd exponent,
power(n, prec)rounds:floorand:ceilingthe wrong way:powerhandlesx < 0as-(-x).power(y, prec). The positive value is rounded first and then negated, so:floorrounds toward zero and:ceilingaway from it. The other modes are symmetric, so only these two are affected.The fix swaps
:floorand:ceiling(insidesave_rounding_mode) for that inner call. The caller's mode is left as it was.Testing
test_power_with_prec_negative_base_floor_ceiling(integer and negative exponents, and checks the mode is restored) fails on master and passes here.bundle exec rake test: 272 tests, 0 failures.x.power(n, prec)calls (n = 1..9, prec 1..40, all 7 modes) checked against the exact power rounded once with Python'sdecimal: master has 1,164 wrong results, all:floor/:ceilingwith a negative result. This branch has none of those.The same run also found 2 results that are off by one unit in the last place for
:half_up/:half_downwith a positive base, on both master and this branch (BigDecimal("4999999999999999999999999995e11").power(9, 6)gives0.195313e349, the exact value rounds to0.195312e349). That comes from rounding twice (once at the extra precision, once at the end), so it's a separate issue and this PR doesn't touch it.