5

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?

Null
  • 1,332
  • 3
    Most calculators use pre-programmed tables of trigonometric functions at certain points, they calculate the rest by various trigonometric formulas and interpolation – Yuriy S Dec 03 '15 at 12:36
  • 5
    This looks essentially identical to my question from a few years back: http://math.stackexchange.com/questions/14066/calculator-algorithms . I would vote to close if I could. – John Smith Dec 03 '15 at 13:35
  • 1
    @JohnSmith Agreed - I just voted to close. An advantage to your question is that it doesn't have a bunch of completely incorrect upvoted and/or accepted answers. – Mark McClure Dec 03 '15 at 13:50
  • Regarding mental calculations, you might be interested in this book: http://www.myreckonings.com/Dead_Reckoning/Dead_Reckoning.htm – Hans Lundmark Dec 03 '15 at 15:04

6 Answers6

14

Here is a page on the TI-Knowledge base asserting that TI calculators use a well established algorithm called the CORDIC algorithm. This algorithm is well described on Wikipedia and in a great paper from the College Math Journal.

To be clear, while Taylor series (referred to in all the previous answers) are very important in numerical analysis they are not the tool of choice for computation of trig functions at arbitrary values.

Mark McClure
  • 30,510
  • Of course this begs the question of where the table of values of arctan came from. :) – Ian Dec 03 '15 at 13:09
  • You could precompute the arctan with a normal Taylor series up to the error you want. – WorldSEnder Dec 03 '15 at 19:50
  • @WorldSEnder Well, almost. Say you want to do the precomputing of the numbers in Wikipedia, you could get $\arctan(2^{-n})$ for $n=1,2,\dots,27$ by Taylor expansion (using at most $\log_2(1/\varepsilon) \sim 53$ terms), but you need to get $\arctan(1)=\pi/4$ by a different method entirely (if you don't want to sum $10^{16}$ terms....) – Ian Dec 03 '15 at 21:17
  • 1
    @Ian: Indeed one way is to use identities such as $1+\tan(t)^2 = \sec(t)^2$. This gives $\tan^{-1}(1) = \cos^{-1}(\frac{1}{\sqrt{2}})$, which we can compute using Taylor series for $\cos^{-1}$. But if you already have an efficient algorithm for any elementary function, you can compute all the rest using such identities and Newton-Raphson with the same efficiency, and the fastest arbitrary precision algorithms have nothing to do with Taylor series, but use the Arithmetic-Geometric Mean iteration instead. – user21820 Dec 24 '15 at 15:32
11

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?

Ian
  • 101,645
  • My guess is that if $mod(50000,2\pi)$ is computed internally using something like $50000-2\pi m$, then the value of $2\pi m$ is going to have an error $2m$ times the error of $\pi$. As $2m = 10^4$ roughly, the error in $x=50000-2\pi m$ would be $10^{-12}$. Such error would produce an error in $sin(x)$ of order $10^{-12}$. This seems to agree with the experimental results. In any case, the operation $50000-A$ with $50000-A\approx1$ is bound to have an error of order $10^{-12}$ just because of the cancellation of digits. So one needs to find a way without subtraction. – timur Jan 12 '16 at 03:45
  • A brute force approach is to do the entire computation $50000-2\pi m$ in a higher precision arithmetic. – timur Jan 12 '16 at 03:49
  • 1
    @timur It turns out there are some relatively sophisticated techniques here; cf. http://www.csee.umbc.edu/~phatak/645/supl/Ng-ArgReduction.pdf. It does involve using some slightly higher precision, but surprisingly not so much, even over the full range of double precision inputs. – Ian Jan 12 '16 at 03:55
6

"and as calculators are invented by human mind can we do it mentally?" Oh, that's a big fallacy. It would probably take you your entire life to calculate what a modern arithmetic circuit can do in a second.

However, there are many ways to "calculate" (i.e. approximate with desired accuracy) trigonometric functions. One way would be linear approximation between lookup tables. Another way would be using the Taylor polynomial.

I'll show the Taylor way for your $\sin(41^\circ) = \sin(\pi \cdot 41/180)$ where $\pi \cdot 41/180$ is slightly smaller than $\pi/4$, so let's develop a first-order Taylor series of $\sin(x)$ about $x = \pi/4$: \begin{align*} \sin(x) &\approx \sin(\pi/4) + \left.\left( \frac{d}{dx} \sin(x) \right) \right|_{x=\pi/4} \cdot (x - \pi/4) \\ &= \sin(\pi/4) + \cos(\pi/4) \cdot (x - \pi/4) \\ &= 1/\sqrt{2} + 1/\sqrt{2} \cdot (x - \pi/4) \end{align*}

In your case

\begin{align*}\sin(41^\circ) &= 1/\sqrt{2} + 1/\sqrt{2} \cdot \pi (41/180 - 45/180) \\ &= 1/\sqrt{2} - 1/\sqrt{2} \cdot \pi \ 4/180 \\ &= 0.6577 \end{align*}

The actual result is $0.6561$, so we are already very close. A calculator would probably use something like a ninth-order Taylor approximation and be extremely close.

GDumphart
  • 2,250
  • 1
    Entire life is an overstatement for TI calculators, which use the fairly low-end Z80 and Motorola 68k microprocessors – qwr Dec 03 '15 at 14:47
3

First of all, to approximate trig functions, it is better to convert the argument to radian measure. Secondly, they use Taylor series to approximate them. For ex:
$$\sin x\approx x-\frac16x^3$$ Gives an answer with $\frac1{400}$ degree of accuracy.
Edit: Radian measure is the basic measure to use in calculus and other branches.$$2\pi=360^{\circ}$$

3

For quick human calculations..

Well, this is probably not the best answer, but you can approximate $\sin 41$ and $\cos 32$ differentials.

For example, we have $$\sin 41^\circ= \sin 45^\circ + \cos 45^\circ \cdot (\frac{41\pi}{180}-\frac{\pi}{4}) = \frac{\sqrt{2}}{2} + \frac{\sqrt{2}}{2} \cdot (-\frac{\pi}{45}) \sim 0.6577$$

In reality it is about $0.6560$. Not bad IMHO.

rkm0959
  • 3,437
2

Most calculators use Taylor's Polynomial approximation to find the value of trigonometric functions.

This is the most simple method to find the value of any function. There could be many other ways to approximate the value of trigonometric functions.

  • For large operands they may first strip out multiples of $360^\circ$ or $2\pi$ or something similar – Henry Dec 03 '15 at 12:36
  • You need to calculate them only for the interval of $(0,\frac{\pi}{2})$ Others are just extension of these. – Prakhar Gupta Dec 03 '15 at 12:46
  • @Henry For sure. – GDumphart Dec 03 '15 at 12:46
  • 4
    This is false, most calculators do not use Taylor series - they use the CORDIC algorithm. – Mark McClure Dec 03 '15 at 13:53
  • 1
    @MarkMcClure - that was once true, but increased processing power has removed the advantages of CORDIC. It is my understanding that only less powerful processors still use CORDIC. So low-end calculators may still use it, but higher-end probably do not anymore. – Paul Sinclair Dec 03 '15 at 16:26