Open vishnusbabu opened 4 years ago
You can use these two const to time the deg/rad to convert:
const RAD2DEG = 180 / Math.pi
const DEG2RAD = Math.pi / 180
For example:
let RightAngleInRad = Math.pi / 2
let RightAngleInDeg = RightAngleInRad * RAD2DEG // 90 deg
@starskyC's comment is generally how you would need to to it. There's no option to switch modes like a calculator might have, since the trig functions are mostly using the built-in JavaScript functions, which only work in radians.
In an expression you can use "PI/180" or "180/PI", but it might be helpful to define constants to make it easier. You can do that by adding them to the Parser's consts property. For example:
var parser = new Parser();
parser.consts.RAD2DEG = 180 / Math.PI;
parser.consts.DEG2RAD = Math.PI / 180;
Then you can use them in expressions, for example:
parser.evaluate('sin(45 * DEG2RAD)');
parser.evaluate('asin(0.7071067811865475) * RAD2DEG');
I'll likely add these (or something similar) as pre-defined constants in the next version to make it easier.
FWIW, this also works if you want the trig functions to work on degrees by default:
// Create a parser, replacing rad based functions with deg based ones:
function getParser() {
const parser = new Parser();
parser.unaryOps.sin = function f(v) { return sin(v * DEGTORAD); };
parser.unaryOps.cos = function f(v) { return cos(v * DEGTORAD); };
parser.unaryOps.tan = function f(v) { return tan(v * DEGTORAD); };
parser.unaryOps.acos = function f(v) { return acos(v) / DEGTORAD; };
parser.unaryOps.asin = function f(v) { return asin(v) / DEGTORAD; };
parser.unaryOps.atan = function f(v) { return atan(v) / DEGTORAD; };
parser.functions.atan2 = function f(u, v) { return atan2(u, v) / DEGTORAD; };
parser.unaryOps.rad = function f(v) { return v / DEGTORAD; };
parser.unaryOps.deg = function f(v) { return v * DEGTORAD; };
parser.unaryOps.swon = function f(v) { return (v > 0) ? 1 : 0; };
parser.unaryOps.swoff = function f(v) { return (v < 1) ? 1 : 0; };
return parser;
}
How can we covert the radian to degree data for all trigonometric function