Skip to content

Simplify power by integer logic #322

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tompng
Copy link
Member

@tompng tompng commented May 17, 2025

Refactor and reduce the number of multiplication.

Example calculation of x**16

x * x**8 * x**4 * x**2 * x # x**8, x**4, x**2 are calculate separately
# ↓
x * x * x**2 * x**4 * x**8 # result of x**4 is re-used to calculate x**8

Calculation of the largest x**n is dominant. Although multiplication is reduced, the performance improvement is limited.

@@ -1660,8 +1660,7 @@ def test_limit
BigDecimal.save_limit do
BigDecimal.limit(1)
x = BigDecimal("3")
assert_equal(90, x ** 4) # OK? must it be 80?
# 3 * 3 * 3 * 3 = 10 * 3 * 3 = 30 * 3 = 90 ???
Copy link
Member Author

Choose a reason for hiding this comment

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

Before: 3**4(3 * (3**2)) * 3(3 * 9) * 3round(27) * 390
After: 3**4(3 * 3) * (3**2)9 * 9round(81)80

Although this test case seems to be improved, just a coincidence.
This pull request is not a bug fix. Large error still exist.

BigDecimal.limit 1
(BigDecimal('2')**100).to_i / 2**100 #=> 1577

@mrkn mrkn modified the milestones: v3.2, v3.3 May 29, 2025
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