Incorrect evaluate with units and multiply/divide #322

Closed
opened this Issue Apr 12, 2015 · 7 comments

Projects
None yet
3 participants

roshanponnappa commented Apr 12, 2015

 math.eval('9km3km') gives an incorrect parse well basically math.eval('9km+3km') works in this way 9(constant)km(operator)+(op,fn)3(constant)(operator) However, things go wrong for math.eval('9km3km') Note: i understand multiply/divide of units are not supported, however parse should work accurately!
Collaborator

FSMaxB commented Apr 12, 2015

 Units are generated on evaluation time, not parse time. The parser simply treats km as a constant, so 9km*3km get's parsed as (9*((km*3)*km)). I don't see anything wrong here apart from how it is (not) evaluated. It's perfectly legitimate mathematically to just treat km as a constant that represents 1km in this case. @josdejong Correct me if I am wrong here. Also this is a case where a separate Node for Units would come in handy. Update: It gets parsed to (9*((km*3)*km)), I updated the above accordingly, but the point still stands. Node tree when parsing 9km*3km: { //OperatorNode "op": "*", "fn":"multiply", "args": [ { //ConstantNode "value":"9", "valueType":"number" }, { //OperatorNode "op":"*", "fn":"multiply", "args":[ { //OperatorNode "op":"*", "fn":"multiply", "args":[ { //SymbolNode "name":"km" }, { //ConstantNode "value":"3", "valueType":"number" } ] }, { //SymbolNode "name":"km" } ] } ] }
Owner

josdejong commented Apr 12, 2015

 @FSMaxB that's correct, something like 9km is parsed as an implicit multiplication: 9*km. Implicit multiplication has the same precedence as explicit multiplication, so 9km*3km is parsed as 9*km*3*km. I see the node tree looks like (9*((km*3)*km)) while I would expect it to parse like (9*(km*(3*km))). I have to figure out why that is as this isn't a predictable order.

roshanponnappa commented Apr 18, 2015

 @josdejong and @FSMaxB thank you for your immediate response, however, if you saw how '9k+3km' is being evaluated correctly.
Owner

josdejong commented Apr 18, 2015

 Yes, that's because the + operator has a lower precedence than the (implicit) multiplication, whilst the * operator has the same precedence as an implicit multiplication.

roshanponnappa commented Apr 18, 2015

 yup, well if anyone is wondering, i have a work around, (9km)*(3km) gets the right evaluation.

Owner

josdejong commented Sep 13, 2015

 I've just fixed the inconsistent order in which implicit and explicit multiplication is parsed, 9km*3km is now parsed as ((9 * km) * 3) * km. As for the original issue: I think this is not relevant anymore, math.js now has support for multiplying units. Shall I close it @roshanponnappa ?

roshanponnappa commented Sep 21, 2015

 Thanks! @josdejong