compute-engine
compute-engine copied to clipboard
Evaluate trig functions with angles in degrees
Hello
Is it possible to force angles to be treated as degrees when evaluating trig functions?
For example, at the moment:
const testExp = ce.parse('\\sin 90');
testExp.N().valueOf();
evaluates as 0.8939966636005579.
However, I would like to set a 'degrees' mode where the answer would be 1.
Thanks.
You can specify the angle in degrees, i.e. ce.parse("\\sin 90\\degree").N().valueOf() returns 1. But there's no way to change the expected argument of the trig functions.
Thanks for the info @arnog . I was really looking for a way to treat angles as degrees in all (potentially deeply nested) trig functions. Maybe some sort of global setting I guess. I suppose I could try some sort of auto-insertion of \\degree before parsing but this seems a bit of a non-optimal thing to do.
+1
@arnog first of all thank you for the great work you do. It's certainly made developing this calculator I'm working on a breeze.
How is this feature coming along? Currently what I've done is parse the latex and insert the \degree string for each trig function. However, when evaluating it seems a challenge to convert the result back to degrees. For example:
acos(cos(45)) + 1 = 1.785...
when it should be
acos(cos(45)) + 1 = 46
The ce.angularUnit property can be used to specify how unitless angle values should be interpreted:
"rad": radians, 2π radians is a full circle"deg": degrees, 360 degrees is a full circle"grad": gradians, 400 gradians is a full circle"turn": turns, 1 turn is a full circle
The default is "rad".
Thank you @arnog for the speedy response. A quick test looks like it doesn't work as expected? Am I doing something wrong here?
EDIT: here is the result with the \degree string removed from the latex:
It's very hard to debug from a screenshot, but it works fine for me (I've simplified the LaTeX: you don't need all those extra braces).
Thank you @arnog will keep testing and hopefully find out what's going on with my setup. In the meantime possibly also another bug, \arccos(cos(0.5))6 doesn't evaluate properly. It should be 3. That's why I had the inverse trig functions in extra parentheses.
The arccos function is spelled "arccos".
Thank you
It's very hard to debug from a screenshot, but it works fine for me (I've simplified the LaTeX: you don't need all those extra braces).
import { ComputeEngine } from '@cortex-js/compute-engine'
const ce = new ComputeEngine()
ce.angularUnit = 'deg'
const latex = '\\arccos(\\cos(45))'
const expr = ce.parse(latex)
const x = expr.N().value
console.log('ce.angularUnit =', ce.angularUnit)
console.log('expr.latex =', expr.latex)
console.log('expr =', JSON.stringify(expr.json))
console.log(`expr.N().value = ${x}`)
The output is:
Am I doing something wrong here? I'm on version "@cortex-js/compute-engine": "^0.24.1"
Thank you for sharing your code. It looks correct. However, those changes are in the main branch but have not been published to npm yet. You can look at the CHANGELOG file for more info.
Ah that makes sense, thank you!
I patched it and it works now. But there now seems to be an issue with tan(90) in degrees which should be infinity. It spits out a very big number this time.
import { ComputeEngine } from '@cortex-js/compute-engine'
const ce = new ComputeEngine()
ce.angularUnit = 'deg'
let latex = '\\arccos(\\cos(45))'
let expr = ce.parse(latex)
let x = expr.N().value
console.log('ce.angularUnit =', ce.angularUnit)
console.log('expr.latex =', expr.latex)
console.log('expr =', JSON.stringify(expr.json))
console.log(`expr.N().value = ${x}`)
console.log('-'.repeat(30))
latex = '\\tan(90)'
expr = ce.parse(latex)
x = expr.N().value
console.log('ce.angularUnit =', ce.angularUnit)
console.log('expr.latex =', expr.latex)
console.log('expr =', JSON.stringify(expr.json))
console.log(`expr.N().value = ${x}`)
I've added support for constructible values when the unit is not radians. Note that tan(90) is ComplexInfinity (or undefined when converting to a JS value).
Thank you very much @arnog working now!
