• Create Account

## How are the trigonometric functions computed?

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

8 replies to this topic

### #1gasto  Members

303
Like
0Likes
Like

Posted 23 July 2014 - 11:09 AM

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?

Edited by gasto, 23 July 2014 - 11:39 AM.

Intel Core 2 Quad CPU Q6600, 2.4 GHz. 3GB RAM. ATI Radeon HD 3400.

### #2SimonForsman  Members

7585
Like
4Likes
Like

Posted 23 July 2014 - 11:38 AM

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.
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!

5832
Like
3Likes
Like

Posted 23 July 2014 - 12:14 PM

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

### #4Álvaro  Members

20255
Like
2Likes
Like

Posted 23 July 2014 - 04:32 PM

Another interesting approach is CORDIC.

### #5Promit  Senior Moderators

12514
Like
0Likes
Like

Posted 23 July 2014 - 04:59 PM

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

SlimDX | Shark Eaters for iOS | Ventspace Blog | Twitter | Proud supporter of diversity and inclusiveness in game development

### #6Ryan_001  Prime Members

3005
Like
0Likes
Like

Posted 23 July 2014 - 08:17 PM

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

Edited by Ryan_001, 23 July 2014 - 10:15 PM.

### #7nobodynews  Members

3016
Like
0Likes
Like

Posted 23 July 2014 - 09:21 PM

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!

### #8plainoldcj  Members

1068
Like
0Likes
Like

Posted 24 July 2014 - 05:45 AM

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.

1880
Like
0Likes
Like

Posted 25 July 2014 - 03:11 PM

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.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.