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

JavascriptMath

Javascript Problem Overview


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?

Javascript Solutions


Solution 1 - Javascript

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)).

Solution 2 - Javascript

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.

Solution 3 - Javascript

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)));
   }
};

Solution 4 - Javascript

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!

Solution 5 - Javascript

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)))

Solution 6 - Javascript

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDavid GView Question on Stackoverflow
Solution 1 - JavascriptPeter OlsonView Answer on Stackoverflow
Solution 2 - JavascriptNiet the Dark AbsolView Answer on Stackoverflow
Solution 3 - JavascriptDaniel BudickView Answer on Stackoverflow
Solution 4 - JavascriptDavid NishikawaView Answer on Stackoverflow
Solution 5 - JavascriptBenjamin AtkinView Answer on Stackoverflow
Solution 6 - JavascriptMLProgrammer-CiMView Answer on Stackoverflow