bigdecimal icon indicating copy to clipboard operation
bigdecimal copied to clipboard

Adding/subtracting bigdecimals with huge differences in exponents hangs

Open tompng opened this issue 2 weeks ago • 0 comments

x = BigDecimal(1)
y = BigDecimal('1e-1000000000000')
x.add(y, 10)
BigDecimal(1).sub(BigDecimal('1e-10000000000'), 10)
# processing time: 0.531971s
# => 0.1e1
BigDecimal(1).sub(BigDecimal('1e-100000000000'), 10)
# processing time: 5.979859s
# => 0.1e1
BigDecimal(1).sub(BigDecimal('1e-9223372036854775808'), 10)
# hangs 20 years(estimated)

Actual case this bug cause problem:

def expm1(x, prec) = BigMath.exp(x, prec).sub(1, prec)
expm1(100000000000, 10) # takes about 4 sec
large_x = 400000
erfc_x = BigMath.erfc(large_x, 10)
erf_x = BigDecimal(1).sub(erfc_x, 10) # takes about 11 sec

tompng avatar Dec 11 '25 16:12 tompng