8

I want to know how to calculate the value of sin, not using table values or calculator.

I found this $\frac{(e^{ix})^2-1}{2ie^{ix}}$, but how to deal with $i$ number, if it's $\sqrt{-1}$?

Teoc
  • 8,700
  • 3
    Well, you could draw a right triangle containing your angle...measure the side opposite the angle and the hypotenuse and take the ratio. I don't see how the exponential will help you...it isn't easy to compute values of that numerically either. – lulu Jul 16 '18 at 10:42
  • 3
    Maybe a truncated Taylor polynomial? – Matti P. Jul 16 '18 at 10:46
  • 1
    Why Do You want to do such a thing? – dmtri Jul 16 '18 at 10:46
  • @lulu, I meant, is there any formula? – bcubeu26dncs Jul 16 '18 at 10:47
  • @ArturKlochko -- I made some small edits to your post to improve readability (at lesat, I hope it does!). If I changed anything you don't like, feel free to put it back, but I hope I've been helpful! :) – Sam OT Jul 16 '18 at 10:48
  • @SamT, appriciate it – bcubeu26dncs Jul 16 '18 at 10:48
  • No formula. There are polynomials that approximate the value. – lulu Jul 16 '18 at 10:49
  • @lulu, I'm not good in math, I dealed with lagrange's polynom. But, as far, as I remember, I need some point's to get it – bcubeu26dncs Jul 16 '18 at 10:53
  • 6
    Perhaps it would help if you edited your post to explain what you are looking for. If you just need the value of the sine of some angle, use a calculator. That machine will inevitably use some numerical algorithms of the sort that machines are good at but people aren't (heavy iterations of elementary operations). Whatever it does is non-trivial. If there were a trivial way to do it, people would use that. – lulu Jul 16 '18 at 10:58
  • Search tip: https://www.google.com/search?q=site:math.stackexchange.com+compute+trig+functions+by+hand – Hans Lundmark Jul 16 '18 at 11:32

8 Answers8

41

You changed very quickly in a comment to MrFatzo's answer to "how do computers calculate $\sin(x)$?", so I'm going to infer that what you're actually trying to ask is:

How does one calculate sines from scratch, without taking anyone's word for the correctness of tables or other magic values that go into the calculation?

I'm aware of two methods:

  1. The ancients reckoned sines in degrees rather than radians. They created tables of sine values (actually chord values, in really ancient times, but that more or less amounts to the same problem) by starting with $\sin(0^\circ)=0$, $\sin(90^\circ)=1$ and then using known formulas for $\sin(v/2)$ to find sines of progressively smaller angles than $90^\circ$, and then formulas for $\sin(v+u)$ to find sines of sums of these smaller angles. That way they could eventually fill out their entire table.

    In this method calculating a single sine from scratch is not really anything you do -- it's not very much less work than creating the entire table, which is to say: years and years of painstaking manual calculations.

    See How to evaluate trigonometric functions by pen and paper? for a bit more detail.

  2. In more modern times -- that is, roughly after the development of calculus -- we prefer our sines in radians. Then the gold standard for what the value of a sine should be is the power series: $$ \sin x = x - \frac16 x^3 + \frac1{120} x^5 - \cdots + \frac{(-1)^n}{(2n+1)!} x^{2n+1} + \cdots $$ This series converges quite fast when $x$ is not larger than a handful of radians, and it is simple to estimate the convergence as you go along (once $2n>x$, the limit will be strictly between any two successive partial sums), so that lets you compute single sines from scratch to any precision you desire.

The power series is still kind of slow even for computers, if you want to compute millions of sines. So in practice computers and calculators use various combinations of clever interpolation methods and tables that are built into the hardware. The tables themselves were ultimately constructed using the power series methods.

  • Thanks a lot, I will read about this approximations – bcubeu26dncs Jul 16 '18 at 11:22
  • 1
    And, ok, I tried to calculate $90-90^3/6$, and $90/180\pi-(90/180\pi)^3/6$ but I didn't get 0. Why? – bcubeu26dncs Jul 16 '18 at 11:24
  • 5
    Actually many pieces of hardware, the low end ones in particular, often use CORDIC-method instead of power series. No costly divisions, you see. I'm not up to speed what exactly "low end" means here currently. Caveat reader. Oh, another user already made the same remark. – Jyrki Lahtonen Jul 16 '18 at 11:28
  • 6
    @ArturKlochko: The power series works for radians, not degrees. So to get the sine of a right angle you should plug in $x=\pi/2$ rather than $x=90$. Just two terms of the series are not enough to get particularly close to $1$, but if you take it up to 4 or 5 terms you should see better agreement. – hmakholm left over Monica Jul 16 '18 at 11:29
  • @JyrkiLahtonen: Yes, that's one of the clever interpolation methods I was alluding to. CORDIC is based on tabulation of a few strategically chosen arctangent values, and I'm assuming that those tables were verified with power series first. – hmakholm left over Monica Jul 16 '18 at 11:30
  • I understood, thank You – bcubeu26dncs Jul 16 '18 at 11:32
  • Ah very cool, I knew about using $\sin(x/2)$ but I never thought of using $\sin(u+v)$ afterwards; it gives infinitely more flexibility. – Ovi Jul 17 '18 at 02:44
19

I'm not sure what can you do "manually", but maybe try using a taylor approximation?
For example, you can calculate $x-\frac{x^3}{6}$

GuySa
  • 538
  • 3
  • 17
  • 2
    Ok, let's ask by another way: how do computers calculate $\sin(x)$ ? – bcubeu26dncs Jul 16 '18 at 10:50
  • Well, that's kind of the opposite of calculating manually.
    I'm not sure what computers do, but maybe they use the taylor approximation up to a certain degree they decided was accurate enough.
    – GuySa Jul 16 '18 at 10:52
  • Don't know what Taylors approximations are, but it seems, that the only good way to get the sin value is to build a right triangle-_- – bcubeu26dncs Jul 16 '18 at 10:55
  • 1
    Although Taylor series (this is, finding the polynomial of degree $\leq n$ that best approximates $\sin$) would work, and perhaps there are other series that converge faster to the function, a quick google search yields the following algorithm: https://en.wikipedia.org/wiki/CORDIC – user347489 Jul 16 '18 at 10:58
  • 5
    My bad, I was assuming you took a calculus 1 class. the idea is that some functions can be represented (at least around a certain point) by an infinite sum. That's called a taylor series. you can also approximate the value instead of calculating it completely by taking the first terms of the infinite sum. Check this list out, for some well-known taylor series: https://en.wikipedia.org/wiki/Taylor_series#List_of_Maclaurin_series_of_some_common_functions – GuySa Jul 16 '18 at 11:00
  • Ok, I tried to calculate $90-90^3/6$, and $90/180\pi-(90/180\pi)^3/6$ but I didn't get 0. Why? – bcubeu26dncs Jul 16 '18 at 11:20
  • 4
    well I kind of lied to you, the formula I gave you is only an approximation and is close to sine only for values close to 0. you can see in the plot here that it is very close to sine but they grow apart as you go further away from 0. I recommend you the identities you know about sine to get to a number close to 0 and then use the formula – GuySa Jul 16 '18 at 11:26
  • Now, i got a complete picture of how to calculate it. Thank You – bcubeu26dncs Jul 16 '18 at 11:31
  • @ArturKlochko: https://math.stackexchange.com/questions/395600/how-does-a-calculator-calculate-the-sine-cosine-tangent-using-just-a-number – Hans Lundmark Jul 16 '18 at 11:34
  • 19
    The formula $x-(x^3/6)$ seemed to fail for $x=90$ because $x$ in this formula is supposed to be the angle measured in radians, not in degrees. So this $90-(90^3/6)$ amounted to an attempt to compute the sine of 90 radians. That, unfortunately, is way outside the range where this truncated Taylor series is anywhere near accurate. – Andreas Blass Jul 16 '18 at 12:25
  • in old games, when personal computers did not have floating-point math processors built in, they actually did use tables. a table of 256 numbers for a quadrant of a circle, then flipped and mirrored, could give you 1024 different directions to 'face' in a game, which was often 'good enough'. – don bright Jul 17 '18 at 09:37
10

Use the old-fashioned method: draw a really big circle, add the angle you wish to calculate, and measure.

  • I wanted to say something along these lines: Draw something resembling a nice sine curve manually and measure... There are a few known special values which can serve as a scaffolding, and the corresponding first derivations. – Peter - Reinstate Monica Jul 16 '18 at 14:19
  • 9
    Unit circle is better. You can draw one cleanly with a compass and don't have to worry about approximation. – Arcanist Lupus Jul 16 '18 at 14:24
  • 1
    You are right ... makes me wonder whether that was ever used in order to assemble sine tables. With a 10m radius circle drawn with a non-elastic rope you can probably get a <1cm measuring accuracy which would be in the 1E-2 - 1E-3 ballpark. – Peter - Reinstate Monica Jul 16 '18 at 14:55
  • 1
    @PeterA.Schneider : The old logarithm and trig. tables were computed by starting with some circle point $s_1=0.01$ (in reality more like $s_1=10^{-6}$) and $c_1=\sqrt{1-s_1^2}\approx 0.99995$ and then doubling the angle with trig. identities until some landmark is passed, for instance $\sin(30°)=\frac12$. Composing the dyadic powers one finds $n$ with $s_n\le\frac12\le s_{n+1}$ and using interpolation one can then reconstruct what angle the pairs $(c_n,s_n)$ correspond to with an accuracy of of about $1e{-}3$ with IMO less effort than required to draw a perfect 10m circle. – Lutz Lehmann Nov 05 '18 at 11:08
5

For a remarkably good approximation with relatively little effort, consider Bhaskara I's sine approximation formula. For angles in degrees (between 0 and 180), this takes the form $$ \sin x^\circ \approx \frac{4x(180-x)}{40500-x(180-x)} $$ while in radians, it's $$ \sin x \approx \frac{16x(\pi-x)}{5\pi^2-4x(\pi-x)}=\frac{16\frac{x}\pi(1-\frac{x}\pi)}{5-4\frac{x}\pi(1-\frac{x}\pi)} $$ This expression has a relative error of less than 2%, which occurs for very small (and for very close to 180$^\circ$) angles. The absolute error is never greater than 0.00165.

Glen O
  • 12,425
4

I would suggest a less well-known method that generalises nicely to many other functions and can be quite efficient even when you need to do all the calculations by hand:

The pair $(c,s) = (\cos x, \sin x)$ (in radians, of course!) can be interpreted as the unique solution to the ordinary differential equation $$ \frac{\mathrm{d}}{\mathrm{d}x} \begin{pmatrix}c\\s\end{pmatrix} = \begin{pmatrix}-s\\c\end{pmatrix} $$ with starting condition $c(0) = 1$, $s(0) = 0$. As such, it can be solved approximately by Runge-Kutta solvers. The idea is to start from zero and then approach the target value step by step, effectively using a Taylor expansion around each point. Because the steps are small, Taylor expansion converges much faster here than if you directly put in the target value.

Why this is particularly convenient for manual calculation: you can choose the step-lengths in a way so the numbers will stay reasonably nice in decimal, as long as you make sure the steps are small and add up to the point where you want to go.

I'll use the Heun scheme, which is the simplest of these iterative solvers that actually gives usable precision. It's based on a 2nd order Taylor expansion.

So let's say you want to compute $\sin (45^\circ) = \sin (\tfrac\pi4)$. We know this should come out as $\sqrt2/2$, but let's see. I'll pick 0.2 as the default step-size. Let's go:
$x_0=0$, $c_0=1$, $s_0=0$

  1. $$\begin{align} h_1=&0.2 \\ x_1=&0.2 \\ \tilde c_1 =& c_0 - h_1\times s_0 = 1 \\ \tilde s_1 =& s_0 + h_1\times c_0 = 0.2 \\ c_1 =& c_0 - \frac{h_1}2\times (s_0 + \tilde s_1) = 1 - 0.1\times0.2 = 0.98 \\ s_1 =& s_0 + \frac{h_1}2\times (c_0 + \tilde c_1) = 0.2 \end{align}$$
  2. $$\begin{align} h_2=&0.2 \\ x_2=&0.4 \\ \tilde c_2 =& c_1 - h_2\times s_1 = 0.98 - 0.2\times0.2 = 0.94 \\ \tilde s_2 =& s_1 + h_2\times c_1 = 0.2 + 0.2\cdot0.98 = 0.396 \\ c_2 =& c_1 - \frac{h_2}2\times (s_1 + \tilde s_2) = 0.98 - 0.1\times 0.596 = 0.9204 \\ s_2 =& s_1 + \frac{h_2}2\times (c_1 + \tilde c_2) = 0.2 + 0.1\times 1.92 = 0.392 \end{align}$$
  3. $$\begin{align} h_3=&0.2 \\ x_3=&0.6 \\ \tilde c_3 =& \ldots = 0.9204 - 0.2\times 0.392 = 0.842 \\ \tilde s_3 =& \ldots = 0.392 + 0.2\times 0.9204 = 0.5768 \\ c_3 =& \ldots = 0.9204 - 0.1\times 0.9688 = 0.82352 \\ s_3 =& \ldots = 0.392 + 0.1\times 1.7624 = 0.56824 \end{align}$$
  4. Ok, if we now did another step of 0.2, we'd end up at 0.8, but $\pi/4 \approx 0.7854$. So we'll only do a step of 0.1. Also I'll start rounding after the fifth decimal here. $$\begin{align} h_4=&0.1 \\ x_4=&0.7 \\ \tilde c_4 =& \ldots = 0.82352 - 0.1\times 0.56824 \approx 0.76670 \\ \tilde s_4 =& \ldots = 0.56824 + 0.1\times 0.82352 \approx 0.65059 \\ c_4 =& \ldots = 0.82352 - 0.05\times 1.21883 \approx 0.76258 \\ s_4 =& \ldots = 0.56824 + 0.05\times 1.53339 \approx 0.64491 \end{align}$$
  5. $$\begin{align} h_5=&0.08 \\ x_5=&0.78 \\ \tilde c_5 =& \ldots = 0.76258 - 0.08\times 0.64491 \approx 0.71099 \\ \tilde s_5 =& \ldots = 0.64491 + 0.08\times 0.76258 \approx 0.70592 \\ c_5 =& \ldots = 0.76258 - 0.04\times 1.47357 \approx 0.70364 \\ s_5 =& \ldots = 0.64491 + 0.04\times 1.35083 \approx 0.69894 \end{align}$$

Ok, we're now pretty close to $\tfrac\pi4$, and it's already good to see how the values for $\cos$ and $\sin$ become very similar, since theory says $\sin(\tfrac\pi4) = \cos(\tfrac\pi4) = \tfrac{\sqrt2}2$. We can further confirm this by quickly squaring: $0.70364^2 \approx 0.495$. Reasonably close to $0.5$. Not great, but also I only did five not-that-small steps (since the Heun scheme is 2nd order accurate, making the steps a bit smaller would give really notably better accuracy) and – that's the main advantage – all of them only involved very simple multiplications, because I could choose the step size so it would be convenient, unlike in a direct Taylor-series evaluation.

That is also the basic idea behind the CORDIC method which was already mentioned in comments. That uses extra properties of sine and cosine to achieve better efficiency, but this is not really needed. Many functions that are defined by an ODE are efficiently evaluated by a Runge-Kutta solver; often the fourth-order version is preferred which gives yet better convergence.

In practice, this method is still only superior to direct Taylor if you need multiple function values. Then it's very good because a) you make a table of values as you go along b) you compute both sine and cosine, which can then be combined by exploiting the symmetry/periodicity properties.

2

There are many ways in which trig functions are calculated by computers, including rather inaccurate ones (for example the fsin method of Intel processors is notorious). A nice overview of implementations of many functions can be found here as part of the GNU MPFR library.

WimC
  • 32,192
  • 2
  • 48
  • 88
1

As for how computer actually evaluate sin(x) and other trig / transcendental functions, rather than using the Taylor series, which can converge rather slowly at times, the method usually used is a Chebyshev Polynomial. It should be noted that the whooshing sound you can hear is the mathematics on that page going clean over my head. ;)

That said, you normally extract a relatively small number of coefficients, and use them in a polynomial expansion that gets reasonable accuracy, albeit with a non-zero error term. This page shows the numbers involved in evaluating Sin(x)

dgnuff
  • 171
0

Well you can use the taylor series or you can use an approximation which I am suggesting, but it will be only useful if you know the value of sin of some nearby anglesh is very less than x

  • Worth elaborating. Use various well known values, plus identities to get values for an angle as near as possible. Then an approximation apporach such as Taylor can converge rapidly and be practical. – Keith Jul 16 '18 at 21:44