farmersckn 122 Report post Posted October 4, 2000 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? 0 Share this post Link to post Share on other sites

^cyer 122 Report post Posted October 4, 2000 hello...just make function out from this, it isnt so hard as it seems..have funarcsin(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 0 Share this post Link to post Share on other sites

Crocmort 122 Report post Posted October 4, 2000 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. =) 0 Share this post Link to post Share on other sites

Guest Anonymous Poster Report post Posted October 4, 2000 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. 0 Share this post Link to post Share on other sites

farmersckn 122 Report post Posted October 4, 2000 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. 0 Share this post Link to post Share on other sites

kseh 3841 Report post Posted October 5, 2000 sin (x - 90) == cos (x) 0 Share this post Link to post Share on other sites

JwayneT 122 Report post Posted October 5, 2000 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. Snootchie Bootchies!-=CF=- 0 Share this post Link to post Share on other sites

capn_midnight 1707 Report post Posted October 6, 2000 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:a^{2}+o^{2}=h^{2}tanx=o/asinx=o/hcosx=a/hcosx=sin(x-90)tanx=sinx/cosxcotx=1/tanx=a/ocscx=1/sinx=h/osecx=1/cosx=h/atan^{2}x+1=sec^{2}xcos^{2}x+sin^{2}x=1cos(2x)=cos^{2}x-sin^{2}x=1-2sin^{2}x=2cos^{2}x-1sin(2x)=2sinxcosxcos(a+b)=cosacosb-sinasinbcos(a-b)=cosacosb+sinasinb (yes i got the orders of the + and - signs right)sin(a+b)=sinacosb+sinbcosasin(a-b)=sinacosb-sinbcosathat''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 upCAN I GET A WOO WOO! 0 Share this post Link to post Share on other sites

Guest Anonymous Poster Report post Posted October 6, 2000 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*xSin(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. 0 Share this post Link to post Share on other sites