4 ms·
How to find an accurate numerical approximation to e, the base of the natural logarithm? Last weekend stumbled onto a shockingly easy and effective way! Googl
by graycat 7y ago
How to find an accurate numerical
approximation to e, the base of the
natural logarithm? Last weekend stumbled
onto a shockingly easy and effective
way!
Google query
"base of the natural logarithm e"
reports
e = 2.718281828459
that is, 13 digits.
The calculator with Windows 10 reports
e = 2.7182818284590452353602874713527
that is 32 decimal digits.
Last weekend found
e = 2.71828182845904523536028747135266250
that is, 36 decimal digits.
The math and code are below and could just
as easily get e to, say, 500 decimal
digits!
How'd that happen?
Last weekend worked on some short but
relatively careful notes to get a nephew
of 9 started on calculus, and part of that
was Taylor series in just two pages with
large fonts!
The code and the core of the Taylor series
derivation are below.
In TeX, Taylor series is
f(x) = \sum_{i=0}^n {(x - x_0)^i \over i!}
f^{[i]}(x_0) + R_n(x_0)
with R_n(x_0) as the error term.
To derive the Taylor series, really just
find the error term
R_n(x_0)
and for that just differentiate f(x) with
respect to x_0 where then nearly all the
terms cancel, simplify, integrate from x_0
to x, and apply the mean value theorem.
That's all there is to it!
The results are, for some s between x_0
and x:
R_n(x_0) = (x - x_0)
{(x-s)^n \over n!} f^{[n+1]}(s)
As above, the final output of the code:
e = 2.71828182845904523536028747135266250
From R_n(x_0) the error is less than
3 x 10^(-40)
The numerical output of the code is
curious: Get a little over 1 decimal
digit of accuracy for each term of the
series! So the output shows two big
triangles, one for the values of n! and
one for the number of correct digits in
the estimate of e.
A key to why this code is so simple and
works so well, Kexx can do arithmetic with
1000 decimal digits of precision!
"Look, Ma, here's the code -- dirt
simple":
macro_name = 'NATLOG'
out_file = macro_name || '.out'
'nomsg erase' out_file
Call msgg macro_name': Find natual logarithm base e'
numeric digits 1000
n = 35
sum = 1
factorial = 1
Do i = 1 To n
factorial = i * factorial
sum = sum + 1/factorial
Call msgg Format(i, 5) Format(factorial, 50) Format(sum, 2, 35)
End
error = 3 / factorial
Call msgg macro_name': The error is <='
Call msgg Format( error, 59, 50 )
Call Lineout out_file
Return
msgg:
Procedure expose out_file
Call Lineout out_file, arg(1)
Return
- Hitton 7y agoThat's Calculus 1.
- graycat 7y agoYes, to quote, as I wrote, > notes to get a nephew of 9 I'm writing the notes for my 9 year old niece. So, the notes are for a boy of 9. And it's calculus, for a boy of 9. Of course it's "calculus 1". I thought I mentioned it was for a boy of 9. So, I omitted (1) a real valued function of a real variable with a compact domain has a Riemann integral if and only it is continuous on a set of measure zero, (2) for such function, if it is differentiable, then it has no jump discontinuities, (3) there is such a function that is differentiable but whose Riemann integral does not exist. But, still, it is amazing how easy it is to get 36 decimal digits of e, and 500 if want. I've been through calculus, advanced calculus, advanced calculus for applications, differential equations, local series solutions to the Navier-Stokes equations, exterior algebra, real analysis, measure theory, and more, have taught calculus in college, applied it in US national security and business, and published in it, and still I'd never seen a clear treatment of how easy it is to get so many digits of e from Taylor series. The results I found are amazing, and my good and long experience indicates that only a tiny fraction of calculus students appreciate that. And as we know, e = lim_{i \rightarrow \infty} (1 + 1/i)^i and it turns out that that iteration is painfully slow, and from my experience this fact is also amazing and poorly known. It was amazing stuff and a productive weekend.