Finding the exponential of a very small number
Show older comments
Hi
I wish to find the exact exponential of numbers in the range of 10^-23. Matlab gives me the answer as 1 for exp(3.528393650226818e-23). This is not right. How do I accomplish this?
Accepted Answer
More Answers (3)
Christoph F.
on 10 Oct 2017
Edited: Christoph F.
on 10 Oct 2017
2 votes
> This is not right.
Is is as close to the correct answer as can be represented with the 64-bit floating point format that Matlab uses.
exp(3.528393650226818e-23) is approximately 1 + 3.528393650226818e-23 (Taylor series, terminated after the first term), which will also give the result "1".
If 64-bit floating point is not enough precision for your purpose, you should look for something that supports 80-bit or quadruple precision (128-bit) floating point data types. Or redefine your task (i.e. how many digits are required?), since a computer will never give you an exact value for a number that has no finite representation.
3 Comments
Himanshi Rani
on 10 Oct 2017
Christoph F.
on 10 Oct 2017
Edited: Christoph F.
on 10 Oct 2017
I do not know of any official support for quadruple precision floating point in Matlab.
However, Jan Simon suggested using variable precision arithmetics, but this requires the symbolic math toolbox (which I do not have available, so I cannot comment on how useful it is for solving your task).
Can you define how many digits of precision you require? Approximating exp(x) as 1+x for very small x is already quite precise, as the next term of the Taylor series, x^2/2, is really tiny if x is small to begin with (if x is ~1e-23, then x^2 is ~1e-46).
Himanshi Rani
on 10 Oct 2017
Edited: Himanshi Rani
on 10 Oct 2017
Walter Roberson
on 10 Oct 2017
Edited: Walter Roberson
on 11 Oct 2017
taylor log(1+exp(x)) to order 5 (so you get the ^4 terms). evaluate at symbol x1 and at symbol x2 and subtract the two -- which will cancel out a log(2) . Multiply by 1.449056603773585e+19 . factor to get
-75471698113207552 * ( x1 - x2) * ( x1^3 + x1^2*x2 + x1*x2^2 - 24*x1 + x2^3 - 24*x2 - 96)
You could work with that, or you could note that in your range 24*x1 or 24*x2 are going to be noise compared to 96, so taking into account floating point accuracy you could reduce this to
-75471698113207552 * ( x1 - x2) * ( - 96)
Multiply out the two constants...
7245283018867924992 * (x1 - x2)
For your s and d, this formula is off in at most the 16th decimal place.
The 5th order taylor series differs from a high precision answer starting at 2^(-70)
3 Comments
Himanshi Rani
on 11 Oct 2017
Edited: Himanshi Rani
on 11 Oct 2017
Walter Roberson
on 11 Oct 2017
x1 is s and x2 is d. 7245283018867924992 * (x1 - x2) is the formula for the complete expression for A
... I might have had a bracket in the wrong place. I will look more carefully after I get some sleep.
Walter Roberson
on 11 Oct 2017
No, I was right. You have
A= (1.449056603773585e+19)*(log(1+exp(s))-log(1+exp(d)))
which is a constant times log(1+exp(s)) - log(1+exp(d))
As s approaches 0, exp(s) approaches 1, so you approach
log(1+1) - log(1+exp(d))
which gets you the log(2) . The same argument for exp(d) with small d, so you headed towards something that very closely approximates log(2) minus something else that very closely approximates log 2. When you taylor the general form log(1+exp(x)) near 0 to get a bit more accuracy, one of the elements in the sum is going to be that expression evaluated at exactly 0, giving log(2); that part of the sum is independent of s or d, so when you subtract the two taylor forms, the log(2) cancel out, leaving only the terms that follow.
taylor(log(1+exp(x)),x,'Order',3)
is x^2/8 + x/2 + log(2) . Do not use direct precision arguments with respect to the log(2) because we are going to cancel those out as described above, so we are left considering x^2/8 + x/2 as the effective value. When x is in the order of 1e-23, then we know that x^2/8 will be on the order of 1e-47 which is negligible compared to 1e-23/2 . We can therefore see that an order 2 approximation is enough: near 1e-23, log(1+exp(x)) is approximately x/2 + log(2) . Substitute in s and d, subtract, and we see that log(1+exp(s)) - log(1+exp(d)) is going to be approximately s/2 - d/2 . All of which is to be multiplied by (1.449056603773585e+19)
Jan
on 10 Oct 2017
The more exact numerical output is - as expected:
1.000000000000000000000035283936502268180000000622478087548...
If you do not want to use VPA and symbolical solutions, you can apply some mathematics also. Look at the Taylor expansion:
exp(x) = 1 + x + x^2/2 + x^3/6 + ...
For x very near to 0.0 this is 1 + x. As Cristoph has explained already 1 + 5.52e-23 cannot be represented "accurately" with the 64 bit IEEE 754 doubles. But perhaps you can proceed with your calculations using the 2 variables:
c1 = 1
c2 = x
and consider, that you need the sum of both.
2 Comments
Himanshi Rani
on 10 Oct 2017
Edited: Himanshi Rani
on 10 Oct 2017
Jan
on 10 Oct 2017
Do you have the Symbolic Toolbox? Unfortunately I do not have it. For a suggestion it would be useful if you explain exactly, what you want to achieve.
Categories
Find more on Special Values in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!