Forum C# Custom Math Atan Script

Custom Math Atan Script

Postby mfranzs » July 6th, 2011, 11:45 pm

Does anyone have a custom math atan script that I can use?

I need it so that I get the same output in both C# and AS3.

Thank you very much!
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Custom Math Atan Script

Postby jasonMcIntosh » July 8th, 2011, 4:48 pm

Are you getting different output from the standard math libs?
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am

Re: Custom Math Atan Script

Postby mfranzs » July 8th, 2011, 8:31 pm

Yeah, the same problem with cosine and sine.

I found a fix though, using infinite series (This is accurate at least into the thousands place, not sure about farther)

Code: Select all
function atan(r)
   {
   return r/(1+(r*r/(3+(4*r*r)/(5+(9*r*r)/(7+(16*r*r)/(9+(25*r*r)/(11+(36*r*r)/(13+(49*r*r)/(15+(64*r*r)/(17+(81*r*r)))))))))));
}
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Custom Math Atan Script

Postby jasonMcIntosh » July 9th, 2011, 12:09 am

Lookup tables, maybe? :) That looks ungainly, if you are using it a lot, but, of course, you should measure before you optimize.
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am

Re: Custom Math Atan Script

Postby mfranzs » July 9th, 2011, 12:37 am

That's a good point, I'm not optimizing yet, but I might come back and improve on this some more.

Code: Select all
function atan(r)
   {
   return r/(1+(r*r/(3+(4*r*r)/(5+(9*r*r)/(7+(16*r*r)/(9+(25*r*r)/(11+(36*r*r)/(13+(49*r*r)/(15+(64*r*r)/(17+(81*r*r)))))))))));
}
var start=(new Date()).getTime();
var sum=0;
for(var i=0;i<1000000;i++){
   sum+=atan(i);
}
trace("Custom: "+((new Date()).getTime()-start));
start=(new Date()).getTime();
sum=0;
for(i=0;i<1000000;i++){
   sum+=Math.atan(i);
}
trace("Built in: "+((new Date()).getTime()-start));


Custom: 1309
Built in: 856


But, if 1 million calculations only take 1.3 seconds... it really doesn't matter THAT much, my calculations arent that intensive.
mfranzs
 
Posts: 98
Joined: August 29th, 2010, 3:27 am

Re: Custom Math Atan Script

Postby Benjaminsen » July 9th, 2011, 5:58 pm

The reason you are getting different results from C# and AS3 is that Flash uses the un standard Number Class which does not have the same precision as neither a float or a duble thus I suggest that you double check that you are getting the same results from your costume code.
Benjaminsen
.IO
 
Posts: 1444
Joined: January 12th, 2010, 11:54 am
Location: Denmark

Re: Custom Math Atan Script

Postby jasonMcIntosh » July 9th, 2011, 9:22 pm

That's a good point, and also a reason that (if you don't need the precision), lookup tables are a sensible/simple option.
Jason McIntosh
Otherwhere Gameworks
jasonMcIntosh
 
Posts: 81
Joined: February 25th, 2011, 4:51 am


Return to C#