Multiple ways to compute e in Raku
blogs.perl.org
blogs.perl.org
What was Perl 6 will, moving forward, be known as "Raku".
Perl is its own thing, definitely, and it does a lot of things extremely well (though not always extremely fast) using code that is as succinct as you want it to be, sometimes to a fault.
It is worth learning because of its utility and because of what it can teach, even though you probably won't use it a lot.
Perl 5 documentation still stands out to me as a perfect example of how official language documentation should be. Lots of examples, lots of explanation, standard library and third party modules are named extremely sanely, and it's all just documented so well. Even joke Perl5 modules are documented extremely well; it's just part of the culture that other languages seriously need to adopt.
Let's say you want to deal with numbers to 1000 decimal places. Pick a base that is a power of 10, such as 10000. Represent a number as an array of 251 base 10000 digits, with the first item of the array being the integer part, and the remaining 250 being 250 base 10000 "decimal" digits.
You need two of these arrays. One represents 1/n!, and the other is the cumulative sum of the series so far.
You need two large precision operations. To go from 1/n! to 1/(n+1)! involves simply dividing by n+1. As long as we limit n+1 to under 10000, we only have to handle division with a single digit denominator. Limiting n thusly is not a problem--it's enough to actually support calculating e to over 35000 decimal places, so our modest goal of 1000 is no problem.
Implement the division exactly like you would do division by a single digit with pencil and paper. The only thing you have to worry about is dealing with carry. You can have intermediate values as large as the carry x the base + base - 1, and since carry can be as large as base - 1, this means you can have intermediate values as large as base^2 - 1. Just pick base (10000 in my example) so that base^2 - 1 doesn't overflow the basic integer type of the language you are using.
The other large precision operation you need is addition, to accumulate the result from the division into the sum. Same as with division--just write it using the same algorithm you would use with pencil and paper. Add digits right to left, with carry.
By using a base that is a power of 10, it is easy to figure out the base 10 representation of the final result for printing.
I think this is important for a language and IMHO current Perl can't compete with that. For a while Ruby had a pretty colorful and productive batch, and of course Go has some elder statesmen of the industry.
JavaScript... Well, it certainly has a lot of coaches.
Sure, because 9/9 = 1 and if you take x = 9^9^9, you get back (1 + x^(-1))^x, i.e. the first formula. It's cute, but I don't know if you could call it a "discovery".
He did in fact build up (1 + x^(-1))^x to slowly increase the accuracy of the formula.
[0] https://www2.stetson.edu/~efriedma/mathmagic/0804.html (The page may say 2004, but has been actively updated, and has references to 2019.)
>>> import math, timeit
>>> x = 2**52
>>> math.e
2.718281828459045
>>> (1+1/x)**x
2.718281828459045
>>> timeit.timeit(lambda: (1+1/x)**x, number = 10**6) / 10**6
2.1324560802895575e-07
>>> timeit.timeit(lambda: (1+1/x)**float(x), number = 10**6) / 10**6
3.0082468304317444e-07
>>> from mpmath import mp
>>> mp.dps = 1000
>>> (1+1/mp.mpf(x))**x
mpf(‘2.7182818284590449335703801338125114…’)
>>> timeit.timeit(lambda: (1+1/mp.mpf(x))**x, number = 10**3) / 10**3
0.0004724335519131273
>>> timeit.timeit(lambda: (1+1/mp.mpf(x))**mp.mpf(x), number = 10**3) / 10**3
0.00047394841397181155
(Times in seconds.) > '𝑒'.uniname
MATHEMATICAL ITALIC SMALL E
> say 𝑒
2.718281828459045[0]: https://stackoverflow.com/questions/30532471/firefox-redirec...
You sure you don't have HTTPS-Everywhere or something similar running?
(There's noting in the response headers or html meta tags that look like it'd be redirecting you too https there...)
More like "Raku (nee Perl6)" I think.
It's (now) called Raku, because it isn't really Perl any more - even though it started out that way. "Perl" is Perl 5 now.