I'm trying to create a detection program for the difference in partial sums between one value of n and the next but I get the error of OverflowError: (34, 'Numerical result out of range'). Is there something I'm not doing right in terms of the type of number stored? This is my code:
def compute_sum(a, b, tolerance=1e-5):
"""Compute the sum $\sum^\infty_{n=1} a^n / b^{n-1} until the partial sums are less than *tolerance*.
Returns the computed sum and raises a *ValueError* if the sum appears to be diverging.
"""
import numpy
n = 1;
R = 100;
Rold = 101;
while (R > tolerance):
while (n == 1):
num = a**n
den = b**(n-1)
n += 1
PS = num/den
PSnew = PS
S = PS
else:
while (Rold > R):
num = a**n
den = b**(n-1)
n += 1
PSold = PSnew
PSnew = num/den
Rold = R
R = abs(PSnew - PSold)
S = S + PSnew
computed_sum = S;
return computed_sum