Large integer rounding problem
2 views (last 30 days)
Show older comments
HAORAN SUN
on 13 Jul 2020
Commented: HAORAN SUN
on 14 Jul 2020
I have a question about an equation when I put it in Matlbab.
We know (1+x/n)^n = e^x as n goes infinity. However, the LHS gives me 1 when I use a small value of x and a large integer value of n(1e15, for example). Is there a better way I can compute this so that the final value is closer to e^x? Thank you so much!
0 Comments
Accepted Answer
David Goodmanson
on 14 Jul 2020
Edited: David Goodmanson
on 14 Jul 2020
Hello HS,
you ran into the limits of double precision numbers. However,
(1+x/n)^n = e^x*f % f = correction factor that is nearly 1 for large n
f = e^(-x)*(1+x/n)^n
% in terms if the log,
log(f) = log(e^(-x)) + log((1+x/n)^n)
= -x + n*log(1+x/n)
% taylor series for log about x = 1
= -x + n*( x/n -(x/n)^2/2 +(x/n)^3/3 -(x/n)^4/4 ...)
= -x^2/(2*n) +x^3/(3*n^2) -x^4/(4*n^3) ...
For large n, the log of the correction factor is -x^2/(2*n) to lowest order. That's the basic result.
You can exponentiate this to get f itself, but again for large n you will run into the limits of double precision. Using the taylor series for exp, you have
f ~~ exp(-x^2/(2*n)) ~~ 1 -x^2/(2*n)
showing the correction factor itself to first order. From this you can estimate how large n has to be until the second term goes to about 1e-15 and is no longer describable in double precision.
You can expand out to many more decimal places with vpa, but storing 1000 decimal places is not nearly as important as knowing the basic behavior for large n.
More Answers (0)
See Also
Categories
Find more on Numbers and Precision in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!