0

How does one compute a number such as np.exp(-28000) on Python? The answer is around 5E-12161. I've been told that due to the double-precision floating point format, I would only be able to calculate a number > 1e-2048

kmario23
  • 57,311
  • 13
  • 161
  • 150
Daniel George
  • 23
  • 1
  • 9

3 Answers3

2

Try mpmath for floating-point arithmetic with arbitrary precision

Edit 1:

>>> import mpmath as mp 
>>> import numpy as np
>>> a = np.matrix((0,0)) 
>>> print(a)
[0.0 0.0]
>>> b = mp.matrix(a.tolist())
>>> c = b.apply(mp.exp)
>>> print(c)
[1.0]
[1.0]   
arduinolover
  • 176
  • 2
  • 5
  • I tried installing that module using pip but it did not show up during compilation. Also, I am trying to find the exponential of an array and I heard mpmath does not do this. – Daniel George Mar 29 '17 at 04:46
  • For mpmath array, array wise operations can be done. Please see the above example – arduinolover Mar 29 '17 at 04:58
  • @arduinolover : This didn't work for numpy arrays and I don't know how to convert the numpy arrays to mp format. – kmario23 Mar 29 '17 at 07:00
  • @arduinolover: Is there an easy way to convert arrays into matrices? Please see my full question: http://stackoverflow.com/q/43100725/6943476 – Daniel George Mar 29 '17 at 18:02
0

Try the decimal module.

Decimal(math.exp(1))**-28000
phg1024
  • 169
  • 6
0

You could define a function to calculate the "bigexponent" and apply it to the array (along an axis). But, note that the input array has to be of type int.

# solution courtesy: http://stackoverflow.com/a/43084475/2956066
In [97]: def bigexp(x):
    ...:     return Decimal(math.exp(1))**x


In [98]: np.apply_along_axis(bigexp, 1, arr)    

Efficiency (in descending order)

# twice faster than applying along axis 0
In [115]: %timeit np.apply_along_axis(bigexp, 1, a)
1000 loops, best of 3: 272 µs per loop

In [116]: %timeit np.apply_along_axis(bigexp, 0, a)
1000 loops, best of 3: 465 µs per loop
kmario23
  • 57,311
  • 13
  • 161
  • 150
  • I tried using your method but am getting this error: ValueError: axis must be less than arr.ndim; axis=1, rank=1. Please see the full question I posted: http://stackoverflow.com/q/43100725/6943476 – Daniel George Mar 29 '17 at 17:57