There is mpmath, numpy's multiprecission cousin:
from mpmath import mp
mp.dps = 100
print(mp.exp(3.832781064493026e-31))
# 1.000000000000000000000000000000383278106449302614152558221812007328689735262269801950763925404522797
print(np.expm1(3.832781064493026e-31))
# 3.832781064493026e-31
print(1+np.expm1(3.832781064493026e-31))
# 1.0
# the second term is eaten away by the 64 bit precission (about 16 decimal digits)
Note that mpmath is quite slower than numpy, doesn't work with numpy's arrays and doesn't mix well with other libraries.
Alternatively, especially if you want to calculate exp(x)-1
, there is numpy's expm1
. This function is introduced because of the precission problems you run into when first calculating exp(x)
and then subtracting 1 for small values of x
. A better explanation can be found in this post and more thoroughly here.