Calculating power of (1-1/x)^y for very large x,y

82 views Asked by At

I am thinking about calculating $(1-1/x)^y$ for very large x,y numbers in python. One way todo so, it to use exp(y*log1p(-1/x)), but I am not sure about its accuracy.

Are they any results on using such accuracy? e.g., bound the error of such result?

I guess it better than using the approximation exp(-y/x)).

1

There are 1 answers

1
SagarNikam On

Definitely there will be accuracy concerns, especially for very large x and y. You can use result = math.expm1(y * math.log1p(-1/x)) this will calculate higher precision for values close to zero. Alternately,egarding error bounds, it's challenging to provide a general bound without knowing specific ranges for x and y. However, you can consider using numerical libraries that provide functions specifically designed for high precision, such as the mpmath library in Python. mpmath allows you to perform arithmetic with arbitrary precision:

from mpmath import mp

mp.dps = 50  # Set the desired precision (adjust as needed)
x=100000000000000000000000000000000000000000000000000000000000000000
y=100000000000000000000000000000000000000000000000000000000000000000
    
result = mp.exp(y * mp.log1p(-1/mp.mpf(x)))

print(result)

#output
Result:0.36787944117144232159552377016146086744581113103177