# How DO you do Sine/Cosine/Log etc algorithmically?

**URL:** <https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217>\
**Category:** Factual Questions\
**Created:** [October 20, 2011, 3:55am UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217 "2011-10-20T03:55:06Z")\
**Posts on this page:** 12\
**Page:** 2

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [October 20, 2011, 10:15pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/21 "2011-10-20T22:15:24Z")

</div>

For what it’s worth, I was assuming the question was “How are the lookup tables constructed in the first place?”. (Of course, one theoretical way of constructing them is to say “Can this be the base 2 log of 7? No, raising 2 to it shows it’s too low. Can this be the base 2 log of 7? No, raising 2 to it shows it’s too high” where the process of raising 2 to a rational power, in turn, involves finding Nth roots of 2 by “Can this be the Nth root of 2? No, taking its Nth power shows it’s too low. Can this be the Nth root of 2? No, taking its Nth root shows it’s too high”. But I assume the question is for a method of computing the lookup tables that is not quite so gorgeously inefficient)

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [October 20, 2011, 10:18pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/22 "2011-10-20T22:18:26Z")

</div>

> [@Indistinguishable](#):
>
> For what it’s worth, I was assuming the question was “How are the lookup tables constructed in the first place?”.

That’s what I thought it was too, and I was incredibly impressed with your answer (mainly because I don’t understand it).

---

<div class="post-metadata">

**Author:** ![AWB](https://avatars.discourse-cdn.com/v4/letter/a/bb73d2/32.png) [@AWB](https://boards.straightdope.com/u/AWB)\
**Post date:** [October 20, 2011, 10:29pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/23 "2011-10-20T22:29:14Z")

</div>

> [@Sage\_Rat](#):
>
> Technically, I ‘invented’ my own measurement rather than using radians or degrees, using 512 units to make a full circle as this was sufficient for my needs and a power of 2, which can always help.

I guess you’d call that unit a “sageratian”, then?

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [October 21, 2011, 7:38am UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/24 "2011-10-21T07:38:07Z")

</div>

I’d just think of it as using units of a full revolution (perhaps the most natural unit for specifying angles…), but encoded via a fixed point datatype with 9 bits of precision past the binary point.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [October 21, 2011, 7:57am UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/25 "2011-10-21T07:57:03Z")

</div>

> [@TriPolar](#):
>
> That’s what I thought it was too, and I was incredibly impressed with your answer (mainly because I don’t understand it).

I could try to explain it better, if you’d like. What parts do you think I should clarify?

---

<div class="post-metadata">

**Author:** ![yabob](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/yabob/32/2821_2.png) [@yabob](https://boards.straightdope.com/u/yabob)\
**Post date:** [October 21, 2011, 2:19pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/26 "2011-10-21T14:19:33Z")

</div>

As for computing them more efficiently, somewhere upstream somebody mentioned Chebyshev. More specifically, you will find a topic called something “economization of power series” studied in numerical analysis courses, which makes use of the Chebyshev polynomials.

One of the problems with the usual power series representations of a function is the distribution of error over the range of x values you will be using. A truncated Taylor series expansion of a function f(x) will be exactly correct at f(a), the point you expanded around. As you move away from that point the error increases. To get desired accuracy for f(x) at the points of your desired range, you may have to use a large number of terms to accommodate the x values furthest from a. The economization techniques allow you to jigger the coefficients of the polynomial obtained from the truncated Taylor series in a way that distributes the error more evenly. This allows acceptable accuracy with a smaller number of terms.

With large memory spaces, fast processors, and the ability to copy a lookup table from somewhere else, economization of series expansions isn’t as important as it once was. At one time it was very important.

---

<div class="post-metadata">

**Author:** ![TriPolar](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/tripolar/32/3008_2.png) [@TriPolar](https://boards.straightdope.com/u/TriPolar)\
**Post date:** [October 21, 2011, 2:33pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/27 "2011-10-21T14:33:52Z")

</div>

> [@Indistinguishable](#):
>
> I could try to explain it better, if you’d like. What parts do you think I should clarify?

Thank you. I would have to spend some time trying to figure it out first. I’m getting an idea of how it operates, but I need to go back and look at the definitions of these functions in the first place. I’d like to take a rain check on the offer though.

---

<div class="post-metadata">

**Author:** ![Chronos](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/chronos/32/134_2.png) [@Chronos](https://boards.straightdope.com/u/Chronos)\
**Post date:** [October 21, 2011, 4:34pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/28 "2011-10-21T16:34:50Z")

</div>

> [@](#):
>
> With large memory spaces, fast processors, and the ability to copy a lookup table from somewhere else, economization of series expansions isn’t as important as it once was. At one time it was very important.

On the other hand, one of my colleagues was recently working on a project which required greater than double precision, and found the existing math libraries for such lacking, and so had to write his own trig functions. IIRC, he ended up using Chebyshev polynomials.

---

<div class="post-metadata">

**Author:** ![md2000](https://avatars.discourse-cdn.com/v4/letter/m/73ab20/32.png) [@md2000](https://boards.straightdope.com/u/md2000)\
**Post date:** [October 21, 2011, 4:37pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/29 "2011-10-21T16:37:43Z")

</div>

The basic formulas are:  
ln(z) = (z-1)- ((z-1)^2)/2+((z-1)^3)/3- … Alternate plus and minus,incresing by 1 each time.

Similarly:  
sin(x) = x - x^3/3!+x^5/5!-x^7/7! etc.  
cos(x)=1-x^2/2!+x^4/4!-x^6/6! etc.

If you are dividing by factorials, and x (radians) is small, then these series will converge pretty fast. The question is whether it is easier and more accurate than a lookup - maybe lookups could take too much memory in the good Olde Days, but today memory is cheap. OTOH, since the 80486, Intel chips have had a separate floating point math coprocessor to handle this sort of calculation. However, if you start to get into extended precision, beyond 8 digits, calculation is probably still your best bet.

---

<div class="post-metadata">

**Author:** ![Indistinguishable](https://avatars.discourse-cdn.com/v4/letter/i/90ced4/32.png) [@Indistinguishable](https://boards.straightdope.com/u/Indistinguishable)\
**Post date:** [October 21, 2011, 5:05pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/30 "2011-10-21T17:05:38Z")

</div>

> [@Indistinguishable](#):
>
> As for where the series comes from, it is constructed to be equal to i times its own derivative and have initial value 1, and thus must compute e[sup]ix[/sup] within its radius of convergence, which is infinite.

Small typo: It is constructed to have derivative i times itself and have initial value 1.

---

<div class="post-metadata">

**Author:** ![BigT](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bigt/32/12044_2.png) [@BigT](https://boards.straightdope.com/u/BigT)\
**Post date:** [October 23, 2011, 2:52pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/31 "2011-10-23T14:52:26Z")

</div>

> [@Chronos](#):
>
> On the other hand, one of my colleagues was recently working on a project which required greater than double precision, and found the existing math libraries for such lacking, and so had to write his own trig functions. IIRC, he ended up using Chebyshev polynomials.

The [ttmath library](http://www.ttmath.org/) can handle at least 2048-bit floats. And it has the trig functions built in.

I wonder what was insufficient about it.

---

<div class="post-metadata">

**Author:** ![ZenBeam](https://avatars.discourse-cdn.com/v4/letter/z/3ab097/32.png) [@ZenBeam](https://boards.straightdope.com/u/ZenBeam)\
**Post date:** [October 23, 2011, 4:33pm UTC](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217/32 "2011-10-23T16:33:28Z")

</div>

> [@Chronos](#):
>
> On the other hand, one of my colleagues was recently working on a project which required greater than double precision, and found the existing math libraries for such lacking, and so had to write his own trig functions. IIRC, he ended up using Chebyshev polynomials.

A long time ago, I had to look into whether we could speed up calculations of Bessel functions. We were using the _Numerical Recipes_ routines, which use rational function approximations. Rational functions are ratios of polynomials, P(x)/Q(x) where P and Q are polynomials. For the Bessel functions, the argument range was 0 to 8, with a different approximation used for larger arguments.

Those were significantly faster than Chebychev polynomial approximations for the same accuracy. The _Numerical Recipes_ functions were only single precision, but if you went to their source, an ancient tome called _Computer Approximations_, they had coefficients for lots of rational function approximations with P(x) and Q(x) of different orders, and the accuracy using each of those. IIRC, some of them had accuracies in the low 20s of digits. That same source had approximations for other functions, probably including Sine and Cosine, but of course I never bothered with those.

[Previous page](https://boards.straightdope.com/t/how-do-you-do-sine-cosine-log-etc-algorithmically/600217.md?page=1)
