72
$\begingroup$
  • Sine $\theta$ = opposite/hypotenuse
  • Cosine $\theta$ = adjacent/hypotenuse
  • Tangent $\theta$ = opposite/adjacent

In order to calculate the sine or the cosine or the tangent I need to know $3$ sides of a right triangle. $2$ for each corresponding trigonometric function. How does a calculator calculate the sine, cosine, tangent of a number (that is actually an angle ?) without knowing any sides?

$\endgroup$
5
  • $\begingroup$ By Taylor approximation, I guess. But I'm not sure. $\endgroup$
    – Julien
    May 18, 2013 at 15:28
  • 14
    $\begingroup$ There is also the CORDIC algorithm $\endgroup$ May 18, 2013 at 15:45
  • $\begingroup$ Also see math.stackexchange.com/q/1239352/13733 $\endgroup$
    – Pacerier
    Jun 22, 2015 at 21:53
  • $\begingroup$ A teacher of mine once said it had a thing to do with Bernstein Polynomials, but didn't elaborate further. He said it was faster than Taylor though. $\endgroup$
    – Evariste
    Nov 2, 2015 at 13:39
  • $\begingroup$ pdfs.semanticscholar.org/f523/… There has been progress since Cordic and Tchebychev methods. Since it costs less in HW, I'm pretty sure many manufacturers of cheap calculators might use it. $\endgroup$
    – zwim
    May 2, 2019 at 14:10

3 Answers 3

50
$\begingroup$

Calculators either use the Taylor Series for $\sin / \cos$ or the CORDIC algorithm. A lot of information is available on Taylor Series, so I'll explain CORDIC instead.

The input required is a number in radians $\theta$, which is between $-\pi / 2$ and $\pi / 2$ (from this, we can get all of the other angles).

First, we must create a table of $\arctan 2^{-k}$ for $k=0,1,2,\ldots, N-1$. This is usually precomputed using the Taylor Series and then included with the calculator. Let $t_i = \arctan 2^{-i}$.

Consider the point in the plane $(1, 0)$. Draw the unit circle. Now if we can somehow get the point to make an angle $\theta$ with the $x$-axis, then the $x$ coordinate is the $\cos \theta$ and the $y$-coordinate is the $\sin \theta$.

Now we need to somehow get the point to have angle $\theta$. Let's do that now.

Consider three sequences $\{ x_i, y_i, z_i \}$. $z_i$ will tell us which way to rotate the point (counter-clockwise or clockwise). $x_i$ and $y_i$ are the coordinates of the point after the $i$th rotation.

Let $z_0 = \theta$, $x_0 = 1/A_{40} \approx 0.607252935008881 $, $y_0 = 0$. $A_{40}$ is a constant, and we use $40$ because we have $40$ iterations, which will give us $10$ decimal digits of accuracy. This constant is also precomputed1.

Now let:

$$ z_{i+1} = z_i - d_i t_i $$ $$ x_{i+1} = x_i - y_i d_i 2^{-i} $$ $$ y_i = y_i + x_i d_i 2^{-i} $$ $$ d_i = \text{1 if } z_i \ge 0 \text{ and -1 otherwise}$$

From this, it can be shown that $x_N$ and $y_N$ eventually become $\cos \theta$ and $\sin \theta$, respectively.

1: $A_N = \displaystyle\prod_{i=0}^{N-1} \sqrt{1+2^{-2i}}$

$\endgroup$
3
  • 1
    $\begingroup$ And despite the algorithm that it uses, the triangle is the one in the unit circle of hypotenuse 1 right? $\endgroup$
    – themhz
    May 18, 2013 at 16:10
  • 3
    $\begingroup$ @themhz, yep. This is the basis for the CORDIC algorithm. So it does actually "make" a triangle in order to find these lengths out. $\endgroup$ May 18, 2013 at 16:14
  • 1
    $\begingroup$ Could one start with $x_0=1$ and then normalize in the final step? $\endgroup$
    – timur
    Jan 12, 2016 at 2:56
5
$\begingroup$

Most implementations of libm for gcc use Chebyshev polynomials. It's faster than Taylor/Maclaurin series and more accurate than Cordics.

$\endgroup$
4
  • 3
    $\begingroup$ As CORDIC, properly implemented, is perfectly accurate to the limits of the machine representation, it is impossible for Chebyshev polynomials to be more accurate. There surely are reasons that Chebyshev polynomials were chosen over CORDIC, but improved accuracy is not among them. $\endgroup$ Dec 3, 2015 at 16:36
  • 1
    $\begingroup$ I suspect that he is talking about Chebyshev interpolation based on a precomputed table. This might be faster if the precision is predetermined. $\endgroup$
    – timur
    Jan 12, 2016 at 2:10
  • 1
    $\begingroup$ @timur They are almost certainly minimax approximations, per Chebyshev equioscillation theorem. Sometimes, a close approximation is obtained simply by Chebyshev economization. $\endgroup$ Jan 12, 2021 at 13:24
  • $\begingroup$ @Jean-ClaudeArbaut: I agree $\endgroup$
    – timur
    Feb 2, 2021 at 15:37
5
$\begingroup$

I always wondered about the same thing, till I attended my first calculus class.

As Julien rightly noted. It uses power series of $\sin x, \cos x$ etc, to only approximately calculate the value of angles(in radians) you put in. You can read more about it here. And power series of $\tan x, \sec x$ and $\text{cosec } x$ is given here.

$\endgroup$
2
  • $\begingroup$ I was just write about the power series of sin,cos etc.I also studied these series in lecture of limit $\endgroup$ May 18, 2013 at 15:39
  • $\begingroup$ @iostream007: I do not think that is necessary, the link here is enough. $\endgroup$
    – Inceptio
    May 18, 2013 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .