How are the trigonometric functions computed?

Started by
7 comments, last by cadjunkie 9 years, 8 months ago

There has been lurking in my mind a question for way too long, without being able to find a suitable, satisfying answer.

How are sin(), cos(), and tan() functions calculated on a calculator or computer?

I understand that mathematicians use the famous unit circle to define the trigonometric functions, but if

sin(?/4)=o/1

o= sin(?/4)*1

a=cos(?/4)*1

tan(?/4)=sin(?/4)

_______

cos(?/4)

So we are still left with the question, how is sine, cosine and tangent computed?

All the books and references that I read basically just suggest to use the calculator, in other words computed by magic or dogma.

Edit: I now remember that they use the Pythagorean theorem.

h²=o²+a²

?=?/4

o=a

1²=2o²

___

?1/2 = o

___

a= ?1/2

___

sin(?/4)=?1/2

_____

1

___

cos(?/4)=?1/2

_____

1

___

tan(?/4)=?1/2

_____

?1/2


But how do they compute the rest of angles?

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.
Advertisement
You could calculate it using http://en.wikipedia.org/wiki/Taylor_series

If you start by doing an argument reduction: sin(n) == sin (n + x*PI) you can get fairly high accuracy quite easily.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Chebyshev polynomials are used too http://en.m.wikipedia.org/wiki/Chebyshev_polynomials they have the nice property that they are bounded in [-1, 1]
"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley
Another interesting approach is CORDIC.

Some hardware also uses interpolation (linear, quadratic, cubic, whatever) between lookup table entries.

SlimDX | Ventspace Blog | Twitter | Diverse teams make better games. I am currently hiring capable C++ engine developers in Baltimore, MD.

Another interesting approach is CORDIC.

I implemented all the trig/power/hyperbolic/ect... functions for my fixed point math library using CORDIC math. Its simple, brilliant, and ridiculously fast. I know it was used in older hardware, whether its still used to this day I am unsure of. But if you like math its well worth learning.

edit: wrote float originall, meant fixed

Or you could go really old-school and try and figure out Ptolemy's Table of Chords. I only got to the first paragraph of the Special Angles section before I decided I have better things to do.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

I understand that mathematicians use the famous unit circle to define the trigonometric functions, but if [...]


Just to make things clear: The trigonometric functions are usually defined analytically. See the
Wiki page.
It can then be shown that these functions give you all the nice properties you already know.
The important point to make is: you _can_ get the value of sin(x) by _computing_ the value of a series!
You're not restricted to fix points and drawings on unit circles!
Most answers above present techniques to approximate the costly computation of the exact values.

Mathematically, the easiest way is using a truncated infinite series and precomputing all the factorials and coefficients you'll need.

Actual computation is highly system-dependent. There are software implementations, like this IBM implementation (the code is complex and divided up into angle intervals with if/then/else statements to be fast) or this fdlibm code (which is easier to read). x86 processors have trig functions implemented in assembly and the FPU uses it's fsin() function.

This topic is closed to new replies.

Advertisement