cortex-js / compute-engine

An engine for symbolic manipulation and numeric evaluation of math formulas expressed with MathJSON
https://cortexjs.io
MIT License
375 stars 47 forks source link

"5d+5" parses as "5e5" #17

Closed bengolds closed 2 years ago

bengolds commented 3 years ago

Perhaps this is some strange scientific notation I'm unfamiliar with, but the latex of 5d+5 parses as { num: '5e5' } instead of [ 'Add', [ 'Multiply', { num: '5' }, 'd' ], { num: '5' } ].

To repro, call:

new LatexParser().parse("5d+5")
arnog commented 3 years ago

Hmmmm.... yes, that's intentional. Using d as the exponent indicator is supported in Fortran and several calculators. I can see how that would be confusing if you expect d to be a symbol instead. Although I guess that ambiguity exists for e as well...

Not sure how to deal with this. I could just drop the d marker. Or make it an option.

bengolds commented 3 years ago

Ooh yeah, that's a tough one. A couple options, none of them great:

  1. Make it an option.
  2. Have different behavior between 5d + 5 and 5d+5 (whitespace matters).
  3. Drop the d marker altogether.

Personally, to keep symmetry with e, I'd pick option 2. 5e+5 and 5e + 5 read quite differently to me!

arnog commented 3 years ago

I think option 2 is already implemented (I haven't verified it, but I remember this was the intent at least). Dunno if it's enough, though...

stefnotch commented 3 years ago

Please do make it an option if possible. I personally have never seen the d indicator and would be quite confused if it ended up parsing differently.

bengolds commented 3 years ago

Looks like option 2 isn't implemented yet:

> c.parse('5d+5')
{ num: '5e5' }
> c.parse('5d + 5')
{ num: '5e5' }
DegrangeM commented 2 years ago

Yea same issue. Using the D variable break everything. I think by default D should be a variable.

strickinato commented 2 years ago

@DegrangeM - I think you're bumping into a different issue (which I also just ran into and made a ticket for): https://github.com/cortex-js/compute-engine/issues/32

Uppercase D and lowercase d seem to have quite different behavior 😄

strickinato commented 2 years ago

0.4.3 appears to either 1 (optional, with dropped d as default) or 3 (completely dropped).

In any case, I think this can probably be closed 😄

> p.parse('5d+5').json
[ 'Add', [ 'Multiply', 5, 'd' ], 5 ]
> p.parse('5d + 5').json
[ 'Add', [ 'Multiply', 5, 'd' ], 5 ]
arnog commented 2 years ago

I went with (3).