# javascript

• 06-15-2007
Redseal
javascript
I'm learning javascript right now and i'm having a bit of trouble.

Code:

``` function components()        {                         var magnitude = document.getElementById("magnitude").innerHTML;                         var angle = document.getElementById("angle").innerHTML;                                                 var xComponent, yComponent;                                                 xComponent = magnitude * Math.cos(angle);                         yComponent = magnitude * Math.sin(angle);                                                                         var myString = "The X componenent is: " + xComponent + " The y Component is: " + yComponent;                                                 alert(myString);                 }```
It keeps giving me the wrong vector components. Am I using the cos and sine functions correctly?

-Redseal
• 06-15-2007
JaWiB
You may have to convert from degrees to radians (rad = deg*pi/180)
• 06-15-2007
mike_g
Are you sure its parsing the other numbers as floats? It should do automatically, but you could enforce it by putting 'magnitude' and the components inside the brackets of parseFloat(). Don't know if that would help....
• 06-15-2007
Redseal
Weird, it works when I convert to radians, any idea why you have to? I never would have thought about the conversion without your help, i was beginning to bang my head against the wall. Thanks!
• 06-15-2007
CornedBee
In most languages, in particular in those derived from C, the math library trigonometry functions deal in radians. That's pretty much just the way it is.
• 06-15-2007
brewbuck
Quote:

Originally Posted by Redseal
Weird, it works when I convert to radians, any idea why you have to? I never would have thought about the conversion without your help, i was beginning to bang my head against the wall. Thanks!

Degrees are a very unnatural unit for measuring angles, whereas radians are the "natural" unit of angle (if you encountered an alien, chances are he would understand what a radian is). So in mathematics, we pretty much always use radians.
• 06-16-2007
ulillillia
I've always found the use/requirement of radians confusing when it comes to angles. A simple macro or function would be all you need to make it easier:

Code:

```#define degrees 0.017453292519943296 double ConvertDegrees(number) {         return(number*0.017453292519943296); } ... value = cos(60*degrees); // both should give exactly 0.5 value = cos(ConvertDegrees(60));```
This is how it'd be set up and used in C. I don't know about javascript though. The first method is the easiest to use and understand.
• 06-16-2007
Happy_Reaper
That doesn't keep radians from being the natural way to measure angles. Sure, you learn how to do it in degrees, first, but once you know radians it's a very backwards way to do things.

For instance, one radian is exactly the angle for there to be exactly an arc length of 1 in the unit circle. As such, it gives you a direct mapping from angles onto arc lengths. There is no such property with degrees.
• 06-16-2007
zacs7
A macro is more efficient ulillillia, although it's not entirely type-safe :(
Code:

```#define DEGTORAD(x) (x * 0.017453292519943296) value = cos(DEGTORAD(60));```
;)

Let's go half way and use gradians :)