thought i'd post this as it may not reach the attention of graphics coders..

this algorithm is useful if you need to increment through sine and/or cosine, eg. when dealing with mapping spheres. there are a number of ways to roll this, including removing one of the multiplies. it's been given all sorts of nice names too. because i've buggered around with several methods, i do not recall the deficit of the single mult form. this way is fairly canonical -

// init
cos = 1.0;
sine = 0.0;
w = (2*pi) / # of steps you want in a period or 2 * pi * f/sr for audio

// loop
cos -= w * sine;
sine += w * cos;

it is unstable at rates higher than pi/4 and, while i have seen all sorts of graphs regarding its accuracy, it's quite applicable for anything you're incrementing less than say 100,000 times.. if you're real worried about that, nudge it every couple hundred/thousand iterations as such, but i found no visual difference compared to transcendentals using it for a 4096 wide image, and don't bother to normalise it running for multiple seconds in audio..

leg = cos * cos + sine * sine;
if (leg exists) {leg = sqrt(leg); cos /= leg; sine /= leg;}

of course cos and sine can be initialised at any correctly paired phase values.


(the single mult form is cos -= k * sin; sin += cos; where k = w * w; but i've only used this for the sine output..)