Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.


manual sin/cos/tan

This topic is 6499 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post

Link to post
Share on other sites
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

Share this post

Link to post
Share on other sites
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. =)

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post

Link to post
Share on other sites
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.

Share this post

Link to post
Share on other sites
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!


Share this post

Link to post
Share on other sites
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:
cos(a-b)=cosacosb+sinasinb (yes i got the orders of the + and - signs right)

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

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
The Maclaurin''s series are not that slow to compute even though they look nasty at first glance.
Here''s the formula, slightly optimized:


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.

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!