5

I need to calculate exp(x**2) where x = numpy.arange(30,90). This raises the warning:

RuntimeWarning: overflow encountered in exp
inf

I cannot safely ignore this warning, but neither SymPy nor mpmath is a solution and I need to perform array operations so a Numpy solution would be my dream.

Does anyone know how to handle this problem?

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
AKG
  • 1,799
  • 4
  • 13
  • 23
  • 1
    For this *particular* case, using `numpy.longfloat` as a NumPy dtype might just work. This depends on your platform, though: if you're lucky, `numpy.longfloat` is the 80-bit IEEE 754 extended precision type, usually padded with zero bytes so that it ends up being reported as `float128` or `float96`. If you're unlucky, it'll just be the same as `numpy.float64`. [If you're *very* lucky indeed, it'll be a genuine 128-bit type.] But what's the use-case? Why do you need to handle such enormous values? – Mark Dickinson Jul 08 '14 at 19:03
  • `numpy.exp(numpy.longfloat(89**2))` -> `1.1132460315637997504e+3440` – Mark Dickinson Jul 08 '14 at 19:08
  • It works perfectly fine, thank you! As input it takes the membrane potential that has characteristic values in that regime. It is just part of a calculation and is multiplied with another term. – AKG Jul 09 '14 at 05:51
  • Hmm. You're dangerously close to the overflow limits, though, so I'd expect that there would still be problems with different input data. (The 80-bit `longfloat` type overflows at around `1.19e+4932`.) I'd definitely be looking for ways to keep track of the logs instead, as @GarethRees suggested. – Mark Dickinson Jul 09 '14 at 05:59

2 Answers2

3

You could use a data type that has the necessary range, for example decimal.Decimal:

>>> import numpy as np
>>> from decimal import Decimal
>>> x = np.arange(Decimal(30), Decimal(90))
>>> y = np.exp(x ** 2)
>>> y[-1]
Decimal('1.113246031563799750400684712E+3440')

But what are you using these numbers for? Could you avoid the exponentiation and work with logarithms? More detail about your problem would be helpful.

Gareth Rees
  • 64,967
  • 9
  • 133
  • 163
3

I think you can use this method to solve this problem:

Normalized

I overcome the problem in this method. Before using this method, my classify accuracy is :86%. After using this method, my classify accuracy is :96%!!! It's great!
first:
Min-Max scaling
second:
Z-score standardization

These are common methods to implement normalization.
I use the first method. And I alter it. The maximum number is divided by 10. So the maximum number of the result is 10. Then exp(-10) will be not overflow!
I hope my answer will help you !(^_^)

intoo
  • 87
  • 4