0

The calculation for which I'm getting the math overflow number is:

e2 = math.exp([[-20.7313399283991]])

There are actually more extreme numbers that I've done than this, why is this causing an overflow?

I get this error:

OverflowError: math range error
user961627
  • 12,379
  • 42
  • 136
  • 210

2 Answers2

2

math.exp() operates on scalars, not on matrices.

You can use it like so, without the square brackets:

>>> math.exp(-20.7313399283991)
9.919584164742123e-10

If you need to operate on a matrix, you could use numpy.exp():

>>> numpy.exp([[-20.7313399283991]])
array([[  9.91958416e-10]])

This computes the element-by-element e**x and returns an array of the same shape as the input. (Note that this is not the same as the matrix exponential; for that, there is scipy.linalg.expm().)

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • Thanks, but when I do `numpy.exp([[-20.7313399283991]])`, I get another error... http://stackoverflow.com/questions/24467970/attributeerror-exp-while-using-numpy-exp-on-an-apparently-ordinary-array – user961627 Jun 28 '14 at 14:49
1

You should call it without the [[]]:

e2 = math.exp(-20.7313399283991)
sshashank124
  • 31,495
  • 9
  • 67
  • 76