compute-engine icon indicating copy to clipboard operation
compute-engine copied to clipboard

Evaluate trig functions with angles in degrees

Open markhats opened this issue 2 years ago • 3 comments

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.

markhats avatar May 26 '23 18:05 markhats

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.

arnog avatar May 26 '23 18:05 arnog

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.

markhats avatar May 30 '23 08:05 markhats

+1

rs-mobitech avatar Sep 22 '23 17:09 rs-mobitech

@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

rwmorton avatar Mar 04 '24 08:03 rwmorton

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

arnog avatar Mar 05 '24 02:03 arnog

Thank you @arnog for the speedy response. A quick test looks like it doesn't work as expected? Am I doing something wrong here?

image

EDIT: here is the result with the \degree string removed from the latex:

image

rwmorton avatar Mar 05 '24 03:03 rwmorton

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

Screenshot 2024-03-04 at 19 40 14

arnog avatar Mar 05 '24 03:03 arnog

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.

image

rwmorton avatar Mar 05 '24 03:03 rwmorton

The arccos function is spelled "arccos".

Uploading Screenshot 2024-03-04 at 20.40.07.png…

arnog avatar Mar 05 '24 04:03 arnog

Thank you

rwmorton avatar Mar 05 '24 04:03 rwmorton

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

Screenshot 2024-03-04 at 19 40 14

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:

image

Am I doing something wrong here? I'm on version "@cortex-js/compute-engine": "^0.24.1"

rwmorton avatar Mar 05 '24 05:03 rwmorton

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.

arnog avatar Mar 05 '24 05:03 arnog

Ah that makes sense, thank you!

rwmorton avatar Mar 05 '24 05:03 rwmorton

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

image

rwmorton avatar Mar 05 '24 06:03 rwmorton

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

arnog avatar Mar 05 '24 16:03 arnog

Thank you very much @arnog working now!

rwmorton avatar Mar 06 '24 04:03 rwmorton