Re: Calculating e^e in Javascript.



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

.



Relevant Pages

  • Re: Javasys.exe: More of the same or new?
    ... Mark wrote: ... > Since the attack starts with a javascript, ... No matter what browser you are using. ... this particular script seems to be looking for Internet ...
    (alt.computer.security)
  • lk-changelog.pl
    ... This script is used by Linus and Marcelo to rearrange and reformat BK ... retrieving revision 0.327 ... 'davidm:hpl.hp.com' => 'David Mosberger', ... 'mark.fasheh:oracle.com' => 'Mark Fasheh', ...
    (Linux-Kernel)
  • VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help
    ... Job is to convert that JAVASCRIPT code into SIMPLE VBA code ... Here's a bookmarklet that will tell you whether or not the browser ... JS sends request to the server (to a PHP script) ... This is the PHP script. ...
    (microsoft.public.vb.controls.internet)
  • VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help
    ... Job is to convert that JAVASCRIPT code into SIMPLE VBA code ... Here's a bookmarklet that will tell you whether or not the browser ... JS sends request to the server (to a PHP script) ... This is the PHP script. ...
    (microsoft.public.inetsdk.programming.webbrowser_ctl)
  • VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted
    ... Job is to convert that JAVASCRIPT code into SIMPLE VBA code ... Here's a bookmarklet that will tell you whether or not the browser ... JS sends request to the server (to a PHP script) ... This is the PHP script. ...
    (microsoft.public.vb.6.webdevelopment)