Re: Calculating e^e in Javascript.
- From: David Golightly <davigoli@xxxxxxxxx>
- Date: Sat, 29 Sep 2007 22:01:28 -0000
On Sep 29, 2:53 pm, Osiro <vinicius.os...@xxxxxxxxx> wrote:
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
.
- References:
- Calculating e^e in Javascript.
- From: Osiro
- Calculating e^e in Javascript.
- Prev by Date: Re: AJAJS - thin client web app using mainly XMLHTTPRequest and eval()
- Next by Date: Re: Calculating e^e in Javascript.
- Previous by thread: Calculating e^e in Javascript.
- Next by thread: Re: Calculating e^e in Javascript.
- Index(es):
Relevant Pages
|