Skip to content

Keep :floor and :ceiling right in power for a negative base - #576

Open
bardrop wants to merge 2 commits into
ruby:masterfrom
bardrop-oss:fix-power-negative-base-floor-ceiling
Open

bardrop wants to merge 2 commits into
ruby:masterfrom
bardrop-oss:fix-power-negative-base-floor-ceiling

Conversation

@bardrop

@bardrop bardrop commented Nov 18, 2026

Copy link
Copy Markdown

With a negative base and an odd exponent, power(n, prec) rounds :floor and :ceiling the wrong way:

BigDecimal.mode(BigDecimal::ROUND_MODE, :floor)
BigDecimal(-3).power(3, 1)          # => -0.2e2  (expected -0.3e2, like BigDecimal(-27).mult(1, 1))
BigDecimal("-0.1234").power(3, 3)   # => -0.187e-2 (expected -0.188e-2)

BigDecimal.mode(BigDecimal::ROUND_MODE, :ceiling)
BigDecimal(-3).power(3, 1)          # => -0.3e2  (expected -0.2e2)

power handles x < 0 as -(-x).power(y, prec). The positive value is rounded first and then negated, so :floor rounds toward zero and :ceiling away from it. The other modes are symmetric, so only these two are affected.

The fix swaps :floor and :ceiling (inside save_rounding_mode) for that inner call. The caller's mode is left as it was.

Testing

  • New 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.
  • 30,000 random x.power(n, prec) calls (n = 1..9, prec 1..40, all 7 modes) checked against the exact power rounded once with Python's decimal: master has 1,164 wrong results, all :floor/:ceiling with 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_down with a positive base, on both master and this branch (BigDecimal("4999999999999999999999999995e11").power(9, 6) gives 0.195313e349, the exact value rounds to 0.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.

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.
Comment thread lib/bigdecimal.rb Outdated
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

No deployments
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.

2 participants