Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get sin, cos, and tan to use degrees instead of radians?

When I'm working with math in JS I would like its trig functions to use degree values instead of radian values. How would I do that?

like image 647
David G Avatar asked Mar 14 '12 15:03

David G


People also ask

Do you use radians or degrees for sin?

Radians. Most of the time we measure angles in degrees. For example, there are 360° in a full circle or one cycle of a sine wave, and sin(30°) = 0.5 and cos(90°) = 0. But it turns out that a more natural measure for angles, at least in mathematics, is in radians.

Why do we use degrees instead of radians?

In particular, rotational motion equations are almost always expressed using radians. The initial parameters of a problem might be in degrees, but you should convert these angles to radians before using them. You should use degrees when you are measuring angles using a protractor, or describing a physical picture.


2 Answers

You can use a function like this to do the conversion:

function toDegrees (angle) {   return angle * (180 / Math.PI); } 

Note that functions like sin, cos, and so on do not return angles, they take angles as input. It seems to me that it would be more useful to you to have a function that converts a degree input to radians, like this:

function toRadians (angle) {   return angle * (Math.PI / 180); } 

which you could use to do something like tan(toRadians(45)).

like image 154
Peter Olson Avatar answered Nov 15 '22 15:11

Peter Olson


Multiply the input by Math.PI/180 to convert from degrees to radians before calling the system trig functions.

You could also define your own functions:

function sinDegrees(angleDegrees) {     return Math.sin(angleDegrees*Math.PI/180); }; 

and so on.

like image 34
Niet the Dark Absol Avatar answered Nov 15 '22 15:11

Niet the Dark Absol