Just had a thought.... How about you store the sin and cosine values in an array from 0 to 360... ( in 1 degree increments ). Then, in your function, linearly interpolate between the two closest angles in the array. So, you'd do something like:
double _cos[360];void InitCosArray(){ for( int i = 0; i < 360; i++ ) { double r = PI/180 * i; _cos[i] = cos( r ); }}// constraints... 0 <= angle < 360// ( the "d_" stands for degrees, a function could also be written for radians ).double d_Cosine( double angle ){ int index1 = (int)angle; // can't get rid of this, at least I don't know how to return _cos[index1] + ( ( angle - index1 ) * ( _cos[index1+1] - _cos[index1] ) );}
|
The accuracy isn't as good as cos(), but it's accurate to about 4 decimal places. The I timed 1000000 cos and d_Cosine calls with the angle of "179.235754". cos() produced "-0.999911" and timed at 160ms, d_Cosine produced "-0.999884" and timed at 130ms.
Marginally faster. If your using whole degree's, just use a look-up table...
[edit: spelling mistake]
Edited by - python_regious on November 7, 2001 11:51:23 AM