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?
David Golightly - 29 Sep 2007 23:01 GMT
> Hi guys!
>
[quoted text clipped - 36 lines]
>
> 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
Thomas 'PointedEars' Lahn - 29 Sep 2007 23:02 GMT
> I created a code below using JavaScript to calculate the function e^e:
>
> <script>
<script type="text/javascript">
> function fat(n)
> {
[quoted text clipped - 27 lines]
>
> 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

Signature
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
Safalra (Stephen Morley) - 30 Sep 2007 11:07 GMT
> I created a code below using JavaScript to calculate the function e^e:
>
[quoted text clipped - 5 lines]
>
> Why does it happen?
Short answer: floating point rounding errors
Long answer: http://docs.sun.com/source/806-3568/ncg_goldberg.html

Signature
Safalra (Stephen Morley)
Sortable Tables In JavaScript:
http://www.safalra.com/web-design/javascript/sortable-tables/
Dr J R Stockton - 30 Sep 2007 15:15 GMT
In comp.lang.javascript message <1191102832.857628.283470@g4g2000hsf.goo
glegroups.com>, Sat, 29 Sep 2007 14:53:52, Osiro
<vinicius.osiro@gmail.com> posted:
>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.

Signature
(c) John Stockton, Surrey, UK. *@merlyn.demon.co.uk / ??.Stockton@physics.org
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Correct <= 4-line sig. separator as above, a line precisely "-- " (SoRFC1036)
Do not Mail News to me. Before a reply, quote with ">" or "> " (SoRFC1036)