title: OverflowError: math range error - Exponent Python
body: <p>I'm trying to create a simple simulated annealing search using python, but always shown an overflow error when calculating the exponent using math.exp.</p>
  
  <p>and this is my code in python:</p>
  
  <pre><code>import random
  import math
  
  def getF(x1, x2):
      t1 = (4 - (2.1 * (x1 ** 2) + (x1 ** 4) / 3)) * (x1 ** 2)
      t2 = x1 * x2
      t3 = (-4 + (4 * (x2 ** 2))) * (x2 ** 2)
      t = t1 + t2 + t3
      return t
  
  
  def getP(dE, t):
      return math.exp((-1*dE)/t)
  
  
  def getNewRandInRange(x):
      newX = x + random.randint(-5, 5)
      while (newX &gt; 10) or (newX &lt; -10):
          newX = x + random.randint(-5, 5)
      return newX
  
  initState1 = random.randint(-10, 10)
  initState2 = random.randint(-10, 10)
  
  currentState1 = initState1
  currentState2 = initState2
  
  BSF = getF(currentState1, currentState2)
  
  T = 1000
  Tmin = 1
  
  while T &gt; Tmin:
      print("T= %f" %T)
      newState1 = getNewRandInRange(currentState1)
      newState2 = getNewRandInRange(currentState2)
  
      currentF = getF(currentState1, currentState2)
      newF = getF(newState1, newState2)
  
      print("Current X1= %f" % currentState1)
      print("Current X2= %f" % currentState2)
      print("New X1= %f" % newState1)
      print("New X2= %f" % newState2)
  
      dE = currentF - newF
      print ("delta E: %f" %dE)
  
      if dE &gt; 0:
          currentState1 = newState1
          currentState2 = newState2
          BSF = getF(newState1, newState2)
      else:
          randNumber = random.uniform(0, 1)
          p = getP(dE, T)
          if (randNumber &lt; p):
              currentState1 = newState1
              currentState2 = newState2
  
      print("BSF: %f" %BSF)
      print("\n\n\n")
      T = T * 0.9
  
  print(BSF) #final output
  </code></pre>
  
  <p>error msg:</p>
  
  <pre><code> Traceback (most recent call last): 
      return math.exp((-1*dE)/t)
  OverflowError: math range error
  </code></pre>
  
  <p>I'm trying using try and catch but it will not return the exponent number and it will makes a problem with the result and also I'm trying googling but doesn't find any solution that meet my requirements. </p>
  
  <p>Thank you before!</p>