manual sin/cos/tan
Ok, so how are sin and cos and tan actually *calculated when you call them from math.h? I mean, if i wanted to create my own sin/cos/tan functions, how would i do it? has anyone ever tried to do this?
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
hello...just make function out from this, it isnt so hard as it seems..have fun
arcsin(x) = x + x^3/(2*3) + 1*3*x^5/(2*4*5) + 1*3*5*x^7/(2*4*6*7) + ... + 1*3*5...(2i-1)x^(2i+1)/(2*4*6*(2i)*(2i+1)) + ...
|x| < 1
arcsin(x) = x + x^3/(2*3) + 1*3*x^5/(2*4*5) + 1*3*5*x^7/(2*4*6*7) + ... + 1*3*5...(2i-1)x^(2i+1)/(2*4*6*(2i)*(2i+1)) + ...
|x| < 1
You''ll have to use the Maclaurin''s Series or something like that. I don''t know of you know what it is... You''ll learn this in Calculus 2 I think. It''s something that look like this:
The MacLaurin Series look like this:
f(x) = f(x) + f''(x)x + (f''''(x)x^2)/2! + (f''''''(x)x^3)/3! + ...
An example for the sin function:
Sin(x) = x - (x^3)/3! + (x^5)/5! - (x^7)/7! + ...
Where the ! mean factorial. And it goes like this to the infiny... You''ll need to know your calculus 1 to understand this... But if you don''t need to be accurate you can only use the first ones(Like 1-5 or something like that). I hope it''ll help you. I don''t have any sites that explain series, I just got this example from my note book of calculus 2. =)
The MacLaurin Series look like this:
f(x) = f(x) + f''(x)x + (f''''(x)x^2)/2! + (f''''''(x)x^3)/3! + ...
An example for the sin function:
Sin(x) = x - (x^3)/3! + (x^5)/5! - (x^7)/7! + ...
Where the ! mean factorial. And it goes like this to the infiny... You''ll need to know your calculus 1 to understand this... But if you don''t need to be accurate you can only use the first ones(Like 1-5 or something like that). I hope it''ll help you. I don''t have any sites that explain series, I just got this example from my note book of calculus 2. =)
October 04, 2000 07:51 PM
One option is to use a look up table. It''d be too simple for precise calculations, but would probably be the fastest.
float Sin(int nIn)
{
float Results[359]={0,0.017,0.0348,0.0523,...};
return Results[nIn];
}
At 180 it just repeats itself but as a negative. Some code could go in between to negate the value or something. We used this method in an assembler class to output a sin wave though we only used every 5th or 10th value or something.
But if you need precision than forget I even said anything and go with the calculus.
float Sin(int nIn)
{
float Results[359]={0,0.017,0.0348,0.0523,...};
return Results[nIn];
}
At 180 it just repeats itself but as a negative. Some code could go in between to negate the value or something. We used this method in an assembler class to output a sin wave though we only used every 5th or 10th value or something.
But if you need precision than forget I even said anything and go with the calculus.
Yikes! That''s pretty nasty stuff for computers. Of course they can do it, but not very fast. I understand it pretty well though. Thank you. Oh, one more question, to get cos, I would just use sin (x - 90), right? Or is it sin (x + 90)? Thanks again.
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
I use the Taylor/MacLaurin series for rotation,
And they work quite fast I might add. What is needed is to compute the values out allready, for things like the factorial. So basically what you get is just multiplication and addition which isn''t that bad on the ol'' CPU. Like I said I use it for realtime sprite rotation and my frame-rate is okay. Mine is out to the fifth member in the series, which is good for 0 to 2pi. All other angles are co-terminal so why bother. But you must remember that YOU MUST USE RADIANS!!!! To convert to degrees is a waste of comp-cycles. Well, that''s my two cents.
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};
And they work quite fast I might add. What is needed is to compute the values out allready, for things like the factorial. So basically what you get is just multiplication and addition which isn''t that bad on the ol'' CPU. Like I said I use it for realtime sprite rotation and my frame-rate is okay. Mine is out to the fifth member in the series, which is good for 0 to 2pi. All other angles are co-terminal so why bother. But you must remember that YOU MUST USE RADIANS!!!! To convert to degrees is a waste of comp-cycles. Well, that''s my two cents.
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};
Snootchie Bootchies!
-=CF=-
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};
Why is it called a hot water heater? Isn't it cold when it goes in the tank?
[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
It depends on the situation. If you are dealing with a triangle, then it''s really easy. And you may be dealing with a triangle and don''t even know it.
You have to be dealing with a right triangle for this to work (two of the sides are perpendicular, or make a 90 degree angle)There are three sides to a right triangle triangle, in relation to the angle you are dealing with. The side opposite the angle is the opposite side (duh), we''ll use ''o'' for short. Then there is the shorter side next to it, called adjacent, ''a'' for short. Finally there is the longest side on the triangle, the Hypotenuse, ''h'' for short. This, of course, is if the angle is not the 90 degree angle. Now, here are some interesting relationships:
a2+o2=h2
tanx=o/a
sinx=o/h
cosx=a/h
cosx=sin(x-90)
tanx=sinx/cosx
cotx=1/tanx=a/o
cscx=1/sinx=h/o
secx=1/cosx=h/a
tan2x+1=sec2x
cos2x+sin2x=1
cos(2x)=cos2x-sin2x
=1-2sin2x=2cos2x-1
sin(2x)=2sinxcosx
cos(a+b)=cosacosb-sinasinb
cos(a-b)=cosacosb+sinasinb (yes i got the orders of the + and - signs right)
sin(a+b)=sinacosb+sinbcosa
sin(a-b)=sinacosb-sinbcosa
that''s about all I can remember. You can use the sides of the triangle to find the values of tan, cos, and sin. in actuallity all you need is sin or cos (both are not necessary) because everything else can be found from there. sin is usually a bit more convenient than cos. Important values to remember sin 0=0, sin 30=sqrt(3)/2, sin 45=sqrt(2)/2, sin 60=1/2, sin 90=1, sin 120=1/2, sin 135=sqrt(2)/2, sin 150=sqrt(3)/2, sin 180=0, sin 210=-sqrt(3)/2, sin 225=-sqrt(2)/2 sin 240=-1/2, sin 270=-1, sin 300=-1/2, sin 315=-sqrt(2)/2, sin 330=sqrt(3)/2 sin 360=sin 0=0. Though sin 0 and sin 180 have the same value, they are not equal. To the immediate left of sin 0 the values of sin become negative, whereas to the left of sin 180 they are positive. To the left of sin 360 they are negative, that is why I wrote sin 360=sin 0.
shut up
CAN I GET A WOO WOO!
You have to be dealing with a right triangle for this to work (two of the sides are perpendicular, or make a 90 degree angle)There are three sides to a right triangle triangle, in relation to the angle you are dealing with. The side opposite the angle is the opposite side (duh), we''ll use ''o'' for short. Then there is the shorter side next to it, called adjacent, ''a'' for short. Finally there is the longest side on the triangle, the Hypotenuse, ''h'' for short. This, of course, is if the angle is not the 90 degree angle. Now, here are some interesting relationships:
a2+o2=h2
tanx=o/a
sinx=o/h
cosx=a/h
cosx=sin(x-90)
tanx=sinx/cosx
cotx=1/tanx=a/o
cscx=1/sinx=h/o
secx=1/cosx=h/a
tan2x+1=sec2x
cos2x+sin2x=1
cos(2x)=cos2x-sin2x
=1-2sin2x=2cos2x-1
sin(2x)=2sinxcosx
cos(a+b)=cosacosb-sinasinb
cos(a-b)=cosacosb+sinasinb (yes i got the orders of the + and - signs right)
sin(a+b)=sinacosb+sinbcosa
sin(a-b)=sinacosb-sinbcosa
that''s about all I can remember. You can use the sides of the triangle to find the values of tan, cos, and sin. in actuallity all you need is sin or cos (both are not necessary) because everything else can be found from there. sin is usually a bit more convenient than cos. Important values to remember sin 0=0, sin 30=sqrt(3)/2, sin 45=sqrt(2)/2, sin 60=1/2, sin 90=1, sin 120=1/2, sin 135=sqrt(2)/2, sin 150=sqrt(3)/2, sin 180=0, sin 210=-sqrt(3)/2, sin 225=-sqrt(2)/2 sin 240=-1/2, sin 270=-1, sin 300=-1/2, sin 315=-sqrt(2)/2, sin 330=sqrt(3)/2 sin 360=sin 0=0. Though sin 0 and sin 180 have the same value, they are not equal. To the immediate left of sin 0 the values of sin become negative, whereas to the left of sin 180 they are positive. To the left of sin 360 they are negative, that is why I wrote sin 360=sin 0.
shut up
CAN I GET A WOO WOO!
[Formerly "capn_midnight". See some of my projects. Find me on twitter tumblr G+ Github.]
October 06, 2000 10:57 AM
The Maclaurin''s series are not that slow to compute even though they look nasty at first glance.
Here''s the formula, slightly optimized:
temp=x*x
Sin(x)=x*(1+temp/(3*2)*(-1+temp/(5*4)*(1+temp/(7*6)*(-1+temp/(9*8)*(1+temp/(11*10)*(-1+temp/(13*12))))))
After you''ve calculated the 3*2=6 and so on, the function above only needs 13 multiplications and some adds. Then you''ll get quite accurate values to sin on range [-pi, pi], which is all you need.
Note that maclaurin series get more inaccurate as the |x| grows.
I personally would prefer the lookup tables with linear or cubic interpolation to get more accuracy.
Here''s the formula, slightly optimized:
temp=x*x
Sin(x)=x*(1+temp/(3*2)*(-1+temp/(5*4)*(1+temp/(7*6)*(-1+temp/(9*8)*(1+temp/(11*10)*(-1+temp/(13*12))))))
After you''ve calculated the 3*2=6 and so on, the function above only needs 13 multiplications and some adds. Then you''ll get quite accurate values to sin on range [-pi, pi], which is all you need.
Note that maclaurin series get more inaccurate as the |x| grows.
I personally would prefer the lookup tables with linear or cubic interpolation to get more accuracy.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement