Reputation: 11
I'm trying to get the exact value from an exponential that gets a very small floating point as input and gives me the result 0.
You can reproduce the problem with the following code:
import numpy as np
from math import sqrt, log, exp, pi
k = np.array([[-746.9292399]])
z = exp(k)
print(z)
The result of this will be 0 , for my pc the result of anything smaller than -743.0 will be 0
I have tried using mpmath to solve this as follows:
import numpy as np
from math import sqrt, log, exp, pi
import mpmath as mp
k = np.array([[-746.9292399]])
z = mp.exp(float(k))
print(z)
det = np.linalg.det([[z,2,3],[2,2,z],[3,6,2]])
print(det)
Which is giving a good answer for the exp() , however i will further need to put that result in a numpy array , and extract the determinant from it , which throws an error in the code i attached because numpy doesn't like mpf numbers in its arrays.
Anybody knows how could i get the result from exp() in a way that i can use it with numpy and its functions?
Upvotes: 1
Views: 1430
Reputation: 30450
This is a common problem with exp
and log
functions, as the output tends to disappear to zero rather quickly. A good observation is that you typically want expr(x) - 1
in most scientific computations, and most implementations provide a custom function for that to avoid numerical stability issues. So, if it fits your use case, you should definitely consider expm1
. Similar considerations also apply to log
, and hence we have log1p
, expm1
, log1pexp
, log1mexp
etc. See here for details: https://docs.scipy.org/doc/numpy-1.13.0/reference/generated/numpy.expm1.html
Upvotes: 2
Reputation: 74
Okay I've been working on this for about 30 mins now and I got this
import decimal
pow(decimal.Decimal(math.exp(1)),decimal.Decimal(-748.9292399))
Also, check this Exponential of large negative numbers
there is one small issue. I calculated your problem with windows calculator and it gave me this 3.819636355e-325
Upvotes: 0
Reputation: 9
it seems to me that around exp(-745)
the value is the smallest that a float can hold.
notice that
>>> exp(-744)
1e-323
The floating point has moved almost entirely to the right. I believe that this is the limit of python's float.
Upvotes: 0