简体   繁体   中英

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?

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

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.

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

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!

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

If you want to have all radian values be converted to corresponding angles in the unit circle (0 degrees <= angle < 360 degrees), you can first modular the angle in radians by 2 * pi and then do the radians to degrees conversion. The same conversion can be achieved in degrees to radians conversion as well and the code is not shown here.

 let radToDeg = function(rad) {
      let pi = Math.PI;
      let smallRad = rad % (2 * pi); // reduce the radians value to be within the unit circle or from 0 to 2 * pi
      let deg = smallRad / (pi) * 180; // normal radians to degrees conversion factor
      let errorBound = 1e-6; // error tolerance
      // adjust floating point imprecisions
      if (Math.abs(deg - Math.round(deg)) <= errorBound){
        return Math.round(deg);
      } else{
        return deg;
      }
    }

To achieve higher precision in the calculation, Javascript's builtin modular (%) operator should be avoided. The following code is basically a copy of the unit circle. However, all angles are converted to their corresponding values in the unit circle. For example, 7.330382858376184 (or pi / 3 + 2 * pi) is converted to 60 degrees; 74774.09394564186 (or -2 * pi / 3 + 23802 * pi) is converted to 240 degrees.

let radToDeg = function(rad) {
  let pi = Math.PI;
  if (evenlyDivide(rad, 2 * pi)) {
    return 0;
  } else if (evenlyDivide(rad, pi)) {
    return 180;
  } else if (evenlyDivide(rad - pi / 2, 2 * pi)){
    return 90;
  } else if (evenlyDivide(rad + pi / 2, 2 * pi)){
    return 270;
  } else if (evenlyDivide(rad - pi / 4, 2 * pi)){
    return 45;
  } else if (evenlyDivide(rad + pi / 4, 2 * pi)){
    return 315;
  } else if (evenlyDivide(rad - pi / 6, 2 * pi)){
    return 30;
  } else if (evenlyDivide(rad + pi / 6, 2 * pi)){
    return 330;
  } else if (evenlyDivide(rad - pi / 3, 2 * pi)){
    return 60;
  } else if (evenlyDivide(rad + pi / 3, 2 * pi)){
    return 300;
  } else if (evenlyDivide(rad - 2 * pi / 3, 2 * pi)){
    return 120;
  } else if (evenlyDivide(rad + 2 * pi / 3, 2 * pi)){
    return 240;
  } else if (evenlyDivide(rad - 3 * pi / 4, 2 * pi)){
    return 135;
  } else if (evenlyDivide(rad + 3 * pi / 4, 2 * pi)){
    return 225;
  } else if (evenlyDivide(rad - 5 * pi / 6, 2 * pi)){
    return 150;
  } else if (evenlyDivide(rad + 5 * pi / 6, 2 * pi)){
    return 210;
  } else{
    let smallRad = rad % (2 * pi);
    return smallRad / (pi) * 180;
  }
}

And the evenlyDivide function is simply checks if the two input values are divisable (result of division of an integer) within some tolerance bounds:

let evenlyDivide = function(val, step) {
  let divided = val / step; // the result of division of two input values
  let errorBound = 1e-7; // error tolerance
  if (Math.abs(divided - Math.round(divided)) < errorBound) { // test if within error bound
    return true;
  }
  return false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM