-
help with a simple calc
can any explaine how can i get a more accurate result
in action script
trace(428855423*428855423)
output:1.83916973836509e+17
getURL("javascript:alert(428855423*428855423)")
output:183916973836508930
-
Hey janiv!
Well, this isn't pretty, but it works .. I'll bet there's an algorithm or two out there that will handle this using logs or bitwise operations, but I was on a roll with String manipulation.
I haven't attempted to optimise it either, so there may be redundancies .. the Flames/Sharks game just started, and this will have to suffice!
Code:
// separate original multiplier into 2 parts:
// 428855423 : 4288e5 and 55423
// multiply value by both multipliers
e = 428855423*4288e5; // 1.838932053824e+17
f = 428855423*55423; // 23768454108929
// capture exponent value of var e
g = Number(String(e).substr(String(e).indexOf("e")+2));
// capture string for linear value of var e
h = String(e).substr(0,String(e).indexOf("e"));
// remove decimal from var e string 'h'
i = h.substr(0,1)+h.substr(2);
// calculate '0' pad length for var e string 'i'
j = g-(h.length-2);
// traces
trace("e:"+e);
trace("f:"+f);
trace("g:"+g);
trace("h:"+h);
trace("i:"+i);
trace("j:"+j);
// '0' pad 'i'
for (var k=0;k<j;k++) {
i += "0";
}
trace("i:"+i);
// length of linear multiplier
l = String(f).length;
// capture var i cropped string
m = i.substr(0,-l);
// capture var i crop
n = i.substr(-l);
// add 2 linear values, then cast to String
o = String(Number(n)+f);
// adjust for +10 condition of left-most digit addition
if (o.length>l) {
// add 1 to value of cropped string 'm', then cast to String
m = String(Number(m)+1);
// remove extra digit from linear addition result
o = o.substr(1);
}
// concatenate for final string display
p = m+o; // 183916973836508929
trace("l:"+l);
trace("m:"+m);
trace("n:"+n);
trace("o:"+o);
trace("p:"+p);
/*
e:1.838932053824e+17
f:23768454108929
g:17
h:1.838932053824
i:1838932053824
j:5
i:183893205382400000
l:14
m:1839
n:93205382400000
o:16973836508929
p:183916973836508929
*/
Note that your JS result rounds the actual value as well (..929 to ..930) .. also due to machine epsilon rounding error.
Richard
Last edited by Dickee; 05-14-2004 at 02:11 AM.
-
Senior Member
Just for curiosity's sake - why are you using actionscript for calculations with such large numbers? Seems like an excellent application for Mathematica or Maple...
-
10x dickee & reason 4 jbum
thanks dickee for your replay i'll try it out.
jbum this a part of encryption code
according to macromedia ac help Number class identical to the js,
which is not as you can see
any way 10x guys
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|