Skip to content

Simplify power by integer logic#322

Closed
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:int_pow_refactor
Closed

Simplify power by integer logic#322
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:int_pow_refactor

Conversation

@tompng

@tompng tompng commented May 17, 2025

Copy link
Copy Markdown
Member

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.

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
Copy Markdown
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
@tompng

tompng commented Jun 12, 2025

Copy link
Copy Markdown
Member Author

This is a small refactoring. It doesn't improve performance.
Closing this because the diff is not important and will be gone away in #347

@tompng tompng closed this Jun 12, 2025
@tompng tompng deleted the int_pow_refactor branch June 12, 2025 14:42
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