Calculating e^e in Javascript.

O

Osiro

Hi guys!

I created a code below using JavaScript to calculate the function e^e:

<script>

function fat(n)
{
f=1;
if (n<0) throw (0);
else if (n==0) return 1;
f = n*fat(n-1);

return f;
}

function mcLaurin(x,steps)
{
ml=0;
for (i=0;i<steps;i++)
ml+= Math.pow(x,i)/fat(i);
return ml;
}

function main()
{
document.write("e^e = "+mcLaurin(Math.E,15));
}

main()
</script>


The result is: e^e = 15.154259237036248

But the same algorithm in Java returned e^e = 15.155343690417329.

Why does it happen?
 
D

David Golightly

Hi guys!

I created a code below using JavaScript to calculate the function e^e:

<script>

function fat(n)
{
f=1;
if (n<0) throw (0);
else if (n==0) return 1;
f = n*fat(n-1);

return f;

}

function mcLaurin(x,steps)
{
ml=0;
for (i=0;i<steps;i++)
ml+= Math.pow(x,i)/fat(i);
return ml;

}

function main()
{
document.write("e^e = "+mcLaurin(Math.E,15));

}

main()
</script>

The result is: e^e = 15.154259237036248

But the same algorithm in Java returned e^e = 15.155343690417329.

Why does it happen?

Math.pow(Math.E, Math.E);

or better

Math.exp(Math.E);

Note that even these two give *slightly* different values, so it's
best to take into account that on no machine is floating-point math
exact. (In fact, your Java-generated value seems even more off the
mark, so there's no telling what the difference may be.)

David
 
T

Thomas 'PointedEars' Lahn

Osiro said:
I created a code below using JavaScript to calculate the function e^e:

<script>

function fat(n)
{
f=1;
if (n<0) throw (0);
else if (n==0) return 1;
f = n*fat(n-1);

return f;
}

function mcLaurin(x,steps)
{
ml=0;
for (i=0;i<steps;i++)
ml+= Math.pow(x,i)/fat(i);
return ml;
}

function main()
{
document.write("e^e = "+mcLaurin(Math.E,15));
}

main()
</script>


The result is: e^e = 15.154259237036248

But the same algorithm in Java returned e^e = 15.155343690417329.

Why does it happen?

Even more rounding errors due to your far-too-complicated approach.

// yields 15.154262241479259 (string-converted) in Firefox 2.0.0.7
Math.pow(Math.E, Math.E);

There are still rounding errors involved due to ECMAScript implementations
using an implementation of IEEE-754 doubles for the Number type.

http://jibbering.com/faq/#FAQ4_7


PointedEars
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
glegroups.com>, Sat, 29 Sep 2007 14:53:52, Osiro
The result is: e^e = 15.154259237036248

But the same algorithm in Java returned e^e = 15.155343690417329.

Why does it happen?


Javascript Math.pow(Math.E, Math.E) gives 15.154262241479259; your
method gives a noticeable rounding error which could be due to internal
rounding at each step.

Javascript uses IEEE Doubles for numbers.

Java van use IEEE Singles (float) or IEEE Doubles (double).

Perhaps you used Java floats, which would have given much larger
rounding errors.

You are using 15 steps, the last being E^14/14! which is about
0.00001379 and the first omitted about 0.0000025 - so you should not be
getting anything close to right anyway. I think you need about 30 steps
to get as accurate an answer as possible in IEEE Doubles.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,404
Latest member
PerryRutt

Latest Threads

Top