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)).
Videos
Can I use Math.sin with degrees?
What does Math.sin return in JavaScript?
Why does Math.sin(Math.PI) not return exactly 0?
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.
Math.sin expects the input to be in radian, but you are expecting the result of 90 degree. Convert it to radian, like this
console.log(Math.sin(90 * Math.PI / 180.0));
# 1
As per the wikipedia's Radian to Degree conversion formula,
Angle in Radian = Angle in Degree * Math.PI / 180
The sin function in Javascript takes radians, not degrees. You need to convert 90 to radians to get the correct answer:
Math.sin(90 * (Math.PI / 180))
Parameters are assumed to be in radians, not degrees.
Try
Math.sin(Math.PI * (30/180));
A comment below notes that pre-computing the ratio ฯ/180 is a good idea. One could add a companion to Math.sin that works on degrees this way:
Math.dsin = function() {
var piRatio = Math.PI / 180;
return function dsin(degrees) {
return Math.sin(degrees * piRatio);
};
}();
(Some people don't like extending built-in objects, but since one doesn't instantiate Math instances โ at least, I don't โ this doesn't seem terribly offensive.)
Math.sin works in radians, I guess your calculator is in degrees.