Skip to content

Calculate division and multiplication with Ruby's Integer for large precision - #380

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

Calculate division and multiplication with Ruby's Integer for large precision#380
tompng wants to merge 1 commit into
ruby:masterfrom
tompng:large_mult_div_use_integer

Conversation

@tompng

@tompngtompng commented Jul 17, 2025

Copy link
Copy Markdown
Member

Ruby's Integer implements faster algorithm (karatsuba, toom3, GMP)
Using Integer's div/mult needs base conversion, but it's still faster when precision is large.

# Mult thresholdprec=150;x=BigDecimal('7'*9*prec);10000.times{x*x}# processing time: 0.867585sprec=149;x=BigDecimal('7'*9*prec);10000.times{x*x}# processing time: 1.041640s# Div threasholdprec=299;y=BigDecimal('7'*9*prec);10000.times{BigDecimal(1).div(y,9*prec)}# processing time: 1.426509sprec=300;y=BigDecimal('7'*9*prec);10000.times{BigDecimal(1).div(y,9*prec)}# processing time: 1.299771s
# Performance degrade case.x=BigDecimal('1'*9*300);(x*7).div(x,9*290)#=> 0.7e1# processing time: 0.016612msx=BigDecimal('1'*9*300);(x*7).div(x,9*300)#=> 0.7e1# processing time: 0.209515ms

…recision
Ruby's Integer implements faster algorithm (karatsuba, toom3, GMP)
@tompng
tompngforce-pushed the large_mult_div_use_integer branch from 67fcd5d to 291ae1cCompareJuly 17, 2025 20:03
@tompng

Copy link
Copy Markdown
MemberAuthor

Closing in favor of #407

Cost of base conversion (not multiplication itself) is quite large for huge n_digits numbers
Example benchmarks:

x=BigDecimal('1'*(10**7));x*x# pull #407: 0.790393s# this patch: 3.530391s# Maximum multiplication supported in #407x=BigDecimal('1'*(9*2**26-1));x*x# pull #407: 28.409065s# this patch: 374.202687sx=BigDecimal('1'*(9*2**24-1));y=BigDecimal('7'*(9*2**23));x.modulo(y)# pull #407: 26.482367s# this patch: 74.415792s

Perhaps this can be used as a fallback of multiplying n_digits > 9*2**26 numbers

@tompngtompng closed this Dec 3, 2025
@tompng
tompng deleted the large_mult_div_use_integer branch December 3, 2025 19:15
Sign up for freeto 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.

1 participant

@tompng