P is for Practical PerlMonks

### Calculating e

by sifukurt (Hermit)
 on Jul 18, 2001 at 05:17 UTC Need Help??

sifukurt has asked for the wisdom of the Perl Monks concerning the following question:

I need some help. I've written a script that will calculate e using Euler's formula. I'm using Math::NumberCruncher to facilitate the factorial process, and Math::BigFloat which should, in theory, allow me to take e out to an arbitrary number of places. However, for some reason it seems to stop after about 320 places, or so. Here is the code:

```use Math::BigFloat;
use Math::NumberCruncher;

\$| = 1;
\$N = shift || 100;
\$e = Math::BigFloat->new("1");
for ( \$i = 1; \$i <= \$N; \$i++ ) {
\$fac = Math::NumberCruncher::Factorial( \$i );
\$e += 1 / \$fac;
}

print \$e, "\n";

Any thoughts on how I can get the above script to go beyond the 320-odd places?

Replies are listed 'Best First'.
Re: Calculating e
by mdillon (Priest) on Jul 18, 2001 at 06:01 UTC

i just used this with N=400 and got a 1638 decimal place e, then with N=500 and got a 2145 decimal place e:

```use Math::BigFloat;
\$N = shift || 100;
\$e = Math::BigFloat->new(1);
\$fac = Math::BigFloat->new(1);
for \$i (1 .. \$N)
{
\$fac *= \$i;
\$e += 1 / \$fac;
}
print \$e, \$/;

my computer is too slow to wait for it to go any higher. i don't know how far you can take it out.

the number of digits seems to increase with the size of N but i don't know why, nor at what rate this happens.

p.s. the result of this code looks to me like e, but i have no idea how accurate it is (beyond being basically accurate, i.e. about 2.71828). i compared it to a 10000 digit calculation of e i found on the Net, and it was only the same up to about 40 decimal places. if you're doing this to get an accurate value of e for later use, i would recommend just finding a calculated and verified version of it on the web (or use the version posted by IO in another response to this parent thread, which, as far as i can tell, reliably generates the actual digits of e). if you're just doing this for fun, then have a blast.

Unfortunately, 1/3! was only accurate to about 40 decimal places
Re: Calculating e
by Albannach (Monsignor) on Jul 18, 2001 at 06:21 UTC
Not only is mdillon's solution correct, it's also much more efficient as each factorial is simply one multiply on the previous one, whereas calling the function each time means a lot more work is being done.

Now the reason for the failure is that the Math::NumberCruncher function overflows for 171!. Unfortunately it returns '1.#INF' which Perl happily treats as 1.0 in your division, so you simply divide by one from that point on, not making much more progress.

Update: While Math::NumberCruncher does not use BigFloats for it's factorial function, it does already use the Math::BigFloat module for storing its very large version of \$PI (though strangely BigFloats are not used in calculations with that \$PI so it's probably pointless...), so it is a trivial matter to patch Math::NumberCruncher to use a BigFloat for the factorial. This is of course for occasional uses as for an iterative use of factorial mdillon's answer is still much faster.

--
I'd like to be able to assign to an luser

Re: Calculating e
by I0 (Priest) on Jul 18, 2001 at 09:58 UTC
```\$|=1;
use integer;
my @e=(1)x450;  #represents sum{\$e[\$i]/\$i!}
for(1..1000){
for(reverse 1..\$#e){    #Normalize
\$e[\$_-1] += \$e[\$_]/\$_;
\$e[\$_] %= \$_;
}
print \$e[0];
\$e[0] = 0;
for( @e ){ \$_ *= 10; }  #shift decimal point
}
Re: Calculating e
by Boots111 (Hermit) on Jul 18, 2001 at 18:40 UTC
This is just a guess based on my knowledge of C and how it treats mathematical terms, but...

I think that the line \$e += 1 / \$fac; is the culprit. While \$e is a BigFloat, my guess would be that the result of the division of 1 and \$fac would be a number of the same type as \$fac. Thus while \$e supports arbitrary precision, 1 / \$fac does not support such a high precision

Matt

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://97512]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (3)
As of 2024-07-24 22:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?
 • erzuuli ‥ 🛈The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.