1
| [code]Actually I managed to speed it up a reasnable amount by getting rid of the sign bit with some loop unrolling |
public static float atan(float x){
//http://en.wikipedia.org/wiki/Arctangent#Infinite_series
//atan(x) = x - x^3/3 + x^5/5
float pow=x;
int n=1;
//int sign=1;
float term=x;
float termABS=Math.abs(term);
float val =term;
while(termABS > precision){
//go up two each time
n+=2;
pow*=x*x;
//sign = sign==1?-1:1;//flip sign every time
float oldTermABS = termABS;
//calc next term
if(n<MAX_EXPANSIONS)
term=/*sign*/pow*recip[n];
else
term=/*sign*/pow/n;
termABS = Math.abs(term);
if(oldTermABS<termABS){
//precision is worsening!
//caused by pow getting very large, and
//factorial getting very small
break;//stop now, things are getting worse!
}
val -= term;
//go up two each time
n+=2;
pow*=x*x;
//sign = sign==1?-1:1;//flip sign every time
oldTermABS = termABS;
//calc next term
if(n<MAX_EXPANSIONS)
term=/*sign*/pow*recip[n];
else
term=/*sign*/pow/n;
termABS = Math.abs(term);
if(oldTermABS<termABS){
//precision is worsening!
//caused by pow getting very large, and
//factorial getting very small
break;//stop now, things are getting worse!
}
val += term;
}
return val;
}
[/code]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| precision = 1.0 average time for API.acos is 1122.7174 av time for FastMath.acos is 155.27678 precision = 0.1 average time for API.acos is 1118.1633 av time for FastMath.acos is 178.82118 precision = 0.010000001 average time for API.acos is 1124.1154 av time for FastMath.acos is 200.12914 precision = 0.0010 average time for API.acos is 1121.4628 av time for FastMath.acos is 227.57458 precision = 1.00000005E-4 average time for API.acos is 1118.9564 av time for FastMath.acos is 241.88194 precision = 1.0000001E-5 average time for API.acos is 1117.9333 av time for FastMath.acos is 263.41922 |