Returns the sine of the radians argument, which we'll call x. This is the most mathematically-intense function, and I have to thank Ian Griffiths again for doing the hard work behind the analysis presented here.
The MacLaurin Series
Altair BASIC's calculation of the sine function is based upon the MacLaurin series. A MacLaurin series is an expansion of functions whose derivatives are continuous.
Now, how do we use this series for the sine function? It's going to look like this :
Well, we know that the derivative of sin(x) is cos(x), and the derivative of cos(x) is -sin(x). We also know that sin(0)=0, and cos(x)=1. Therefore the series can be reduced to :
Simplifying a bit further, and limiting the number of terms to a reasonable approximation, we get down to :
Scaling
To make the code in this and the next section smaller, our first step is to scale our radians argument x into a new variable u that is in the range -1 < u< 1. To do this we first divide x by 2p and then lose the integer part of that number.
Quadrantization
The second part of the algorithm is an optimisation to reduce u to a 'quadrantized' value, q, so called because it lies within the two quadrants either side of the origin, ie -0.25<q<0.25. Here's a little graph showing sin(x) against u, with the quadrant numbers shown in red.
To get from u to q, we don't need to scale again but we do take advantage of two trignometric identities : sin(-x) = -sin(x), and sin(x)=sin(pi-x).
<fixme: insert Monte's explanation here>
So we have our value q. Here's another graph showing sin(x) against the quadrantised value q.
fixme: the comments inlined below are train-of-thought and should not be used. I haven't quite worked this out.
x is between -0.75 and 0. Here we add 0.5 to get x between -0.25 and 0.25 and signal that negation is required
Progression Calculation
<fixme>
The modified Taylor series used by SIN.