cosinus function

Started by
25 comments, last by Jesper T 22 years, 5 months ago
(i<<1) == (i * 2)

right ?

..but theres is no angle checking




(I want to guive yuo teh impresion that I am very intelgient)
Advertisement
For sine/cosine something I tried was to create two rather large arrays of 3600 floats each, and filled them with the appropriate trig function values for 0.0 to 359.9 in .1 increments. Then, to find the sines I just used a macro that cast the argument to an integer and multiplied by ten to get the right array index.

It could probably be sped up considerably by reducing the "resolution" to 1/8 or maybe even 1/4 increments and then instead of multiplying by ten, just shifting to the left a bit.
Cosine is also defined as an infinite series I can''t think of right now... don''t use it, because it won''t be accurate unless you expand it to the first five terms, and then it''s slow to caculate.
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
If at first you don't succeed, redefine success.
Yeah, that looks fast, but the main problem (I think) is to check if the angle is grater than 2PI/360 and then reduce it, it requires some time, but maybe not so much if checking integers.. dunno.. oh, and btw, does anybody know if it is possible to do this:
  value = -value  


by just changing the sign bit ?

I tried this:

  ((DWORD*))[1] &= 0x7FFFFFFF;  


.. couldnt make it work.. I have no clue what those signs are.. well some, I guess the F''s are hex..
That is not a valid character. To do it correctly, try this:

    //Take some negative floating point valuefloat value = -7.05f;//Calculate the absolute value by zeroing out the sign bit(*((DWORD *)&value)) &= 0x7FFFFFFF;  


Perhaps your browser changed the &ang to a sort of code.

Edited by - Kippesoep on November 12, 2001 4:57:21 AM
Kippesoep
ah k, cool thanks, thatll probably speed it up slightly




while(consciousness){ while(beer){ drinkBeer(); if(sick){ vomit(); } } while(money){ buyBeer(); } if(consciousness && !money) { if((bool)(rand() % 2)){ stealBeer(); } else { stealMoney(); } } }

This topic is closed to new replies.

Advertisement