title: Python infinite series convergence and divergence
body: <p>I am trying to write a program that calculates the sum of the infinite series of a^n / b^(n-1) from n = 1 to infinity and terminates and raises a value error when it diverges. Here's what I have:</p>
  
  <pre><code>def compute_sum(a, b, tolerance=1e-5):
  
  n=1.
  computed_sum = 0.
  previous_sum = 0.
  
  while (n&gt;0.):
      previous_sum = computed_sum
      computed_sum += a**n / b**(n-1.)
      n=n+1.
      if (computed_sum - previous_sum &lt; tolerance or computed_sum - previous_sum &lt; -tolerance):
          raise ValueError()       
  
  return computed_sum
  </code></pre>
  
  <p>The loop is supposed to terminate when the difference between the computed sum and the previous is sum is less than a specified tolerance. If I deleted the if statement and computed it with a=-2 and b=3, and using a while(n&lt;9999) I get a close answer. </p>
  
  <p>I'm just not sure how to exit the loop if it diverges or the difference is less than a given tolerance level.</p>
  
  <p>Am I at least headed in the right direction or is my approach completely wrong? What's the methodology here?</p>
  
  <p>Thank you!</p>