What's new

Calculating Sine

BGNG

New member
It has come to my attention several times that computers are able to generate the sine of an angle given only the angle. While I know Sine is circular in nature, I do not know how the computers manage to do this.

Does anyone happen to know the math behind the Sine function? I know the basic trigonometry "Y / R" concept, but how is a computer able to take "Sin(Pi / 2)" and turn it into 1?
 

cufunha

New member
it's a little hard to explain every little thing if you don't know lots of math. I'll give you the basics:

given a right triangle (a triangle which the max angle has 90º or Pi/2 radians), the sine of the angle x is defined as the quocient between the oposite side and the hipotenuse (side in front of the right angle).

With this definition, we can calculate some values of sine for some angles:

1) 30º = Pi/6: imagine you have an equilateral triangle (all of the three sides have the same length). The three internal angles of this triangle have 60º (Pi/3 radians). Now, draw a bissetrix of one of these angles and it will divide your triangle into two right triangles. To calculate the sine of 30º, take the angle divided by the bissetrix (60/2=30) and make oposite side over hipotenuse: l/2/l = 1/2.

2) 60º = Pi/3: take the same divided triangle of above. Sin 60º = length of the bissetrix (use pithagoras theorem to find that it's l*sqrt(3)/2) over hipotenuse (l): so sin 60º = sqrt(3)/2;

3) 45º = Pi/4: take a right triangle with internal angles equal to 45º, 45º and 90º. Using pithagoras theorem, we find that the hipotenuse is equal to the others sizes plus sqrt(2). So, Sin 45º = l/l*sqrt(2) = 1/sqrt(2) = sqrt(2)/2.


there are some formulas involving sine and cossine:

sin(2x) = 2sin(x)cos(x);
sin(x + y) = sin(x)cos(y) + sin(y)cos(x);
sin(x - y) = sin(x)cos(y) + sin(y)cos(x);

to find sin(90º=Pi/2), just do it:
sin 90º = sin (2*45º) = 2sin(45º)cos(45º) = 2*sqrt(2)/2 * sqrt(2)/2 = 1; (obs: the cossine of 45º = sqrt(2)/2);

but, how computers calculate, for example, Sin(18*Pi/107), Sin(0.235), Sin(100), etc?

When, every funtion in the world can be approximated to a polinomial when the value you wanna calculate is near some known value. The formula is that:

:homestar: f(x) = f(a) + f’(a)*(x-a) + f’’(a)*(x-a)²/2! + f’’’(a)*(x-a)³/3! + f’’’’(a)*(x-a)^4/4! + ...
if x->a; (f’ is the first derivative, f’’ is the second derivative, etc...)

if we apply this aproximation for a point x near to 0 (a=0) using the sine funtion, we have:

sin(x) = sin(0) + cos(0)*x - sin(0)*x²/2 - cos(0)*x³/6 + sin(0)*x^4/24 + ...

but sin(0) = 0 and cos(0) = 1 (just use the formulas above to get this).
So, if x is near to 0, sin(x) = x - x³/6 + ...

computers use this formula to calculate sine, another one to calculate cossine and another to calculate the tangent.

:homestar: But, if the angle x is not near 0. If x=61*Pi/3, for example?
Well, take the formula sin(x+y) = sin(x)cos(y)+sin(y)cos(x) to find that
sin(61Pi/3) = sin(20Pi + Pi/3) = sin(20Pi)cos(Pi/3) + sin(Pi/3)cos(20Pi) = Sin(Pi/3), because Sin(20Pi) = 0 and Cos(20Pi) = 1.

I hope you have undestood something... There's a lot more to explain, but these are the basics.

Go to this site:

http://mathworld.wolfram.com/Sine.html
 
Last edited:

Hacktarux

Emulator Developer
Moderator
the processor is simply using the taylor's formula (or another formula build on the same idea) to approximate trigonometric functions and all other functions that the processor can compute. As this function can only be used accurately locally around a known point, i guess there are some hardcoded values on the processor but i'm not sure.
 
Last edited:
OP
BGNG

BGNG

New member
Ah. I had completely forgotten about MathWorld; otherwise I would have looked there. Thanks for the heads-up, cufunha.

It was from MathWorld that I previously looked up the equation for the Euler constant e and the math behind Square Root.

Also, wouldn't you be able to calculate Cosine by adding Pi / 2 to the argument and calculating the resulting Sine? And Tangent would simply be Sin/Cos...
__________

Oh well. Thanks for the tips, guys.
 

Cyberman

Moderator
Moderator
Hacktarux said:
the processor is simply using the taylor's formula (or another formula build on the same idea) to approximate trigonometric functions and all other functions that the processor can compute. As this function can only be used accurately locally around a known point, i guess there are some hardcoded values on the processor but i'm not sure.
Actually they don't use Taylor's forumula per sea. They use COORDIC. COORDIC is a coordinate transformation method that maps an angle into a region that can be computed with a short taylor equation within the precision you want, then it remap the result to the original angle, taking far fewer computations to do. This is how the original PC's coprocessor did there trigonmetric functions, I use to have the paper done by the Intel engineer who developed the trig functions for the X87. COORDIC engines are now common as dirt in computers. I doubt any machine these days uses a direct taylor equation because of the massive performance hit for computing a sine with that number of calculations. COORDIC reduces it from 5000 additions divisions and multiplications to I believe 17 which can be done in parallel. Look up coordic transformations sometime. I did a lot of research in computer graphics in the early stages of the PC .. as an end result of my devient mind I guess :D
A brief of what COORDIC involves.
 

Top