title: Different output from same code with exponents in python
body: <p>I'm running some code in a loop, which runs for many thousands of data points, where at one point I have this line of code:</p>
  
  <pre><code>z[i, j] = -math.exp(oneminusbeta[j, i])
  </code></pre>
  
  <p>I put comments <strong>before</strong> each line of code, and the last line comment outputted before the code crashed was this:</p>
  
  <pre><code>z[30416,20] = -math.exp(oneminusbeta (which is 8.04812689598e-13))
  </code></pre>
  
  <p>The error was:</p>
  
  <pre><code>OverflowError: math range error
  </code></pre>
  
  <p>Which means that the above line caused the crash. 
  However, when I open a separate console and type this</p>
  
  <pre><code>a = -math.exp(8.04812689598e-13)
  print str(a)
  </code></pre>
  
  <p>My code doesn't crash. It just outputs:</p>
  
  <pre><code>-1.0
  </code></pre>
  
  <p>What's going on? Why is it that in the middle of a bunch of code, raising an exponent to this power makes the program crash, but it doesn't crash otherwise? What can I do to prevent this? Or how do I know when python will decide some exponent is too much to deal with and suddenly crash?</p>
  
  <h2>Edit</h2>
  
  <p>Similar to the way I initialized z, I just tried this: </p>
  
  <pre><code>a = np.empty([1,1]) 
  a[0,0] = -math.exp(8.04812689598e-13) 
  print str(a)
  </code></pre>
  
  <p>But this doesn't crash either.</p>
  
  <h2>Edit 2</h2>
  
  <p>Someone suggested that the error might be caused by the line after the comment output.
  The line after the one I mentioned looks like this:</p>
  
  <pre><code>weights[i,j] = math.exp(beta[j,i] + oneminusbeta[j,i])
  </code></pre>
  
  <h2>Edit 3</h2>
  
  <p>I ran the code again and whilst in the above example, the code crashed on <code>weights[30816, 42]</code>, this time it crashed on <code>weights[55399, 43]</code>. Which means that this time it didn't overflow on the same spot it overflowed above, but in another spot. Which means that it isn't really an issue of python not being able to do the calculation.</p>
  
  <p>But I'm using exactly the same dataset as before.
  What's going on and is there any solution to this arbitrary issue? I'm using Anaconda Spyder on a 64-bit machine.</p>