大负数的指数

Exponential of large negative numbers

如何计算 Python 上的 np.exp(-28000) 这样的数字?答案在5E-12161左右。有人告诉我,由于双精度浮点格式,我只能计算一个数字 > 1e-2048

试试十进制模块。

Decimal(math.exp(1))**-28000

尝试mpmath进行任意精度的浮点运算

编辑 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]   

您可以定义一个函数来计算 "bigexponent" 并将其应用于数组(沿轴)。但是,请注意输入数组的类型必须是 int.

# solution courtesy: 
In [97]: def bigexp(x):
    ...:     return Decimal(math.exp(1))**x


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

效率(降序)

# 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