Help with cos, sin and tan^-1 (arctan);

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 04-21-2007
xIcyx
Help with cos, sin and tan^-1 (arctan);
I'm writing a program using cos, sin and arctan. However, Whenever the program computes something it fails at doing cos and sin properly. I have not written it to the arctan stage yet.

Here is what I'm using:

angle is inputed by the user. This value is between 0 and 90.

value is also inputed by the user. This value is any positive value

Code:

``` forcex = value * cos(angle); forcey = value * sin(angle);```
When I unput a value of 90 and a angle of 0, I get the correct answer. However, if I input the value of 90 and have an angle of 45 I get: 47 for x and 76 for y.

I'm using the following .h files:

math.h
stdio.h
stdlib.h

What exactly am I doing wrong here? Do I need to convert the degrees to radians or is there anyway I can keep it in degrees?
• 04-21-2007
Cactus_Hugger
sin, cos, etc. use radians. Are you converting?
• 04-21-2007
xIcyx
That explains it, as I was writing this post I realized it may have to be in radians.... I'm going to have to convert it but honestly I don't remember how to convert degrees to radians. I know that pi/2 is 90, pi is 180, 3pi/2 is 270 and pi is 360... but I really don't know how to convert it a "proper way". Any idea how?
• 04-21-2007
whiteflags
Pi over 180 degress is one degree, and 180 degrees over pi is one radian. Thus

To convert from degrees to radians multiply degrees by πrad / 180deg
• 04-21-2007
xIcyx
Quote:

Originally Posted by citizen
Pi over 180 degress is one degree, and 180 degrees over pi is one radian. Thus

To convert from degrees to radians multiply degrees by πrad / 180deg

I'll use this:

Thank you for your help guys.
• 04-21-2007
xIcyx
I'm kind of stuck again guys... How exactly do I use arctan (tan^-1) in C.

I tried:

direction = arctan(totaly/totalx);

However, the compiler does not recognize arctan. What do I use instead to make this work?
• 04-21-2007
OnionKnight
In C the trigonometric inverses are called asin, acos and atan.
Take a look inside your math.h
• 04-21-2007
ulillillia
I hate the fact radians are used. The trig functions using angles always use radians so it might be good to create a macro, function, or something else that converts from degrees to radians and back if you're more comfortable with degrees than radians. The arc functions return an angle in radians and the nonarc ones request an angle in radians.

#define radians 57.295779513082321 // enough precision for a double
#define degrees 0.017453292519943296

Just multiply by these numbers to convert:

Variable = sin(30*degrees); // 30 degrees should give 0.5
AnotherVariable = atan(Y/X)*radians // the angle returned is in degrees this way
• 04-22-2007
zacs7
The reason why radians are used is for precision,

@xIcyx perhaps read math.h or look-up whats in it? Rather than guessing...
• 04-22-2007
jabka
a diffrent way is to use taylor sentence :
Code:

```f(x) = f(0) + f(0)' / 1! + f(0)^''/2! + f(0)^''' / 3! .. + f(0)^n / {n(x-0)!} sin a = sum from{n = 0} to {infinity} = {(-1)^n over (2n + 1)! }  x^{2n+1}```
• 04-22-2007
brewbuck
Quote:

Originally Posted by ulillillia
I hate the fact radians are used.

People who hate radians are mathematically ignorant. They didn't just make this crap up for no reason you know.
• 04-22-2007
Happy_Reaper
Agreed. I even wonder why they put degrees in there in the first place. However, I do agree they should put a macro or function in there somewhere for converting (even though it's ridiculously easy).
• 04-22-2007
robatino
> The reason why radians are used is for precision,
It has nothing to do with precision. Radians are the only "natural" angle measure - for example, derivative formulas for trig functions would have to include a constant factor != 1 if the angle was measured in any other units, and the Taylor series for the trig functions would have to include the same constant factor - for example, using radians, (d/dx) sin(x) == cos(x), and sin(x) == x - x^3/3! + x^5/5! - ... Try working out how to modify these formulas if x is measured in degrees. It's similar to the fact that e is the only "natural" base for logarithms (the same reasons apply).
• 04-22-2007
ulillillia
I understand what radians are and how the term is defined. Given a circle with a radius of 1 unit, the angle of one radian is where the circumference would equal one unit (or that of the radius), which is about 57.3 degrees. The degree is based on Earth's orbit around the Sun. It was first believed to be 360 days in the year and that's how the 360-degree circle came about. Search Wikipedia for more details on radians and degrees.
• 04-22-2007
OnionKnight
Radians are very useful, and I'm disturbed by the fact that OpenGL uses degrees. Try for example, deriving a trigonometric function using degrees.
cos(x)' = -sin(x)*0.017453292519943295769236907684886
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last