TITLE: How calculators do trigonometry
QUESTION [5 upvotes]: I need to use a calculator to find trigonometric ratios like $\sin(41),\cos(32)$.
How do calculators work for trigonometry? Also, as calculators are invented by the human mind can we do it mentally? Is there any binary trick or something like that?

REPLY [11 votes]: Generally speaking, for routines that are used over and over like those for trig functions, calculators and computers have a table of precomputed values and use those to generate other values. The details of this depend on the problem. A common scheme is to use polynomial interpolation or piecewise polynomial interpolation of computed values of the desired functions. The CORDIC algorithm is often used for trig functions in particular, and it does something else, which is nicely discussed in the Wikipedia article. Maybe this is of interest to you, but I always find it a bit unsatisfying when I get these answers, because they usually don't say where the precomputed values came from.
Here is a simple algorithm for computing $\sin$ with little precomputing required. It's based on Taylor approximation. To make it work, you need to already know $\pi$, and you also need to precompute the number of Taylor terms that will be used. The latter is discussed below.
First we'll use some basic trigonometric identities to reduce to a function sin1 which is restricted to the first quadrant. (This is written in Matlab syntax.)
function y=mysin(x)
z=mod(x,2*pi);
if 3*pi/2<=z
  y=-sin1(2*pi-z);
elseif pi<=z
  y=-sin1(z-pi);
elseif pi/2<=z
  y=sin1(pi-z);
else
  y=sin1(z);
end

Now we will reduce that to the first half of the first quadrant. We can do that by just computing cosine after reflecting through the line $y=x$.
function y=sin1(x)
if x>pi/4
  y=cos1(pi/2-x);
  return
end

So let's deal with the ones that didn't need to get reduced to cosine. Here Taylor's theorem tells us
$$\sin(x)=\sum_{n=0}^N \frac{(-1)^n x^{2n+1}}{(2n+1)!} + R_N$$
where $|R_N| \leq \frac{|x|^{2N+2}}{(2N+2)!}$. So we need to identify $N$ such that $\frac{|\pi/4|^{2N+2}}{(2N+2)!}<10^{-16}$ (around the usual error tolerance for IEEE double precision). Just directly checking some values reveals that it is enough to take $N=8$. So we can finish the second half of our "sin1" function like this:
n=0:8;
m=2*n+1;
y=sum((-1).^n.*x.^(m)./factorial(m));

Then you can do basically the same thing to define cos1, to finish the overall problem:
function y=cos1(x)
n=0:8;
m=2*n;
y=sum((-1).^n.*x.^(m)./factorial(m));

Note that this can be made considerably more efficient without actually changing the mathematical character of it by precomputing the coefficients and evaluating the polynomial using Horner's method.
Note that not all the reductions that I did here were strictly necessary. We do need to reduce to one period (otherwise the method would need to dynamically choose $N$ and would be very slow for large arguments). But if we do that and don't reduce to the first quadrant, we can just raise $N$ to $20$. Alternately, we could just reduce to the first quadrant itself and stop, provided we raise $N$ to $11$. 
Also, I tested the above a little bit. It deviates a little bit from Matlab's sin function for large arguments; for instance sin(50000)= -0.999840189089790 and mysin(50000)= -0.999840189089824. The problem appears to be entirely caused by arithmetic error in the first step, since (within Matlab) sin(mod(50000,2*pi))==mysin(50000). Perhaps someone could comment on a good way to fix this?