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?
-
25sin/cos/tan don't return angles in radians; they take angles in radians. The inverse functions `atan()`, `atan2()`, `acos()`, `asin()` return angles. – Jason S Mar 14 '12 at 16:23
6 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))
.

- 139,199
- 49
- 202
- 242
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.

- 378
- 2
- 8

- 320,036
- 81
- 464
- 592
-
2This should really be named `sinDegrees` to avoid confusion – BlueRaja - Danny Pflughoeft Mar 14 '12 at 15:55
-
True. I've seen some sites call is `sind` to keep it short. I personally keep it to `sin` because I know that `sin` will then be in degrees and `Math.sin` in radians, but that's on my own head. – Niet the Dark Absol Mar 14 '12 at 16:00
-
7
-
2That... that one got away from me. I know that. Of course I know that. I wouldn't have made a bazillion games with trig functions if I hadn't... but for some reason I kinda completely forgot that =/ – Niet the Dark Absol Mar 14 '12 at 17:35
-
I'm a noob and bad with trig...may I ask how the equation for cosDeg and tanDeg would look like? thank you. – Kama Feb 12 '13 at 02:52
-
-
I tried to get the cosine of 90 using Math.cos(90/180*Math.PI). the answer is 6.123233995736766e-17. When it should be 0 more or less. – Kama Feb 12 '13 at 03:35
-
1The `e-17` tells you that it's `0.00000000000000006123...` – Niet the Dark Absol Feb 12 '13 at 05:00
I created my own little lazy Math-Object for degree (MathD), hope it helps:
//helper
/**
* converts degree to radians
* @param degree
* @returns {number}
*/
var toRadians = function (degree) {
return degree * (Math.PI / 180);
};
/**
* Converts radian to degree
* @param radians
* @returns {number}
*/
var toDegree = function (radians) {
return radians * (180 / Math.PI);
}
/**
* Rounds a number mathematical correct to the number of decimals
* @param number
* @param decimals (optional, default: 5)
* @returns {number}
*/
var roundNumber = function(number, decimals) {
decimals = decimals || 5;
return Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals);
}
//the object
var MathD = {
sin: function(number){
return roundNumber(Math.sin(toRadians(number)));
},
cos: function(number){
return roundNumber(Math.cos(toRadians(number)));
},
tan: function(number){
return roundNumber(Math.tan(toRadians(number)));
},
asin: function(number){
return roundNumber(toDegree(Math.asin(number)));
},
acos: function(number){
return roundNumber(toDegree(Math.acos(number)));
},
atan: function(number){
return roundNumber(toDegree(Math.atan(number)));
}
};

- 28,539
- 11
- 85
- 132

- 1,780
- 1
- 18
- 19
I like a more general functional approach:
/**
* converts a trig function taking radians to degrees
* @param {function} trigFunc - eg. Math.cos, Math.sin, etc.
* @param {number} angle - in degrees
* @returns {number}
*/
const dTrig = (trigFunc, angle) => trigFunc(angle * Math.PI / 180);
or,
function dTrig(trigFunc, angle) {
return trigFunc(angle * Math.PI / 180);
}
which can be used with any radian-taking function:
dTrig(Math.sin, 90);
// -> 1
dTrig(Math.tan, 180);
// -> 0
Hope this helps!

- 61
- 1
- 1
-
That's not especially functional or general. What if it takes two arguments? What if it returns something in radians (`Math.asin()` for example) and you want to get degrees as output? The accepted answer is using just the right amount of functional programming. An unnecessary use of a function as an argument doesn't make it a functional programming approach. – Benjamin Atkin Aug 17 '21 at 19:41
There's a project with more than a thousand stars on GitHub that provides functions for converting from degrees to radians and radians to degrees.
To install:
npm i @stdlib/math
To import:
const rad2deg = require('@stdlib/math/base/special/rad2deg')
const deg2rad require('@stdlib/math/base/special/deg2rad')
To use:
console.log(Math.sin(deg2rad(90)))
console.log(rad2deg(Math.asin(1)))

- 14,071
- 7
- 61
- 60
Create your own conversion function that applies the needed math, and invoke those instead. http://en.wikipedia.org/wiki/Radian#Conversion_between_radians_and_degrees

- 17,231
- 5
- 42
- 75
-
7Whilst this may theoretically answer the question, [it would be preferable](http://meta.stackexchange.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Jason C Mar 22 '14 at 01:38