New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect evaluate with units and multiply/divide #322

Closed
roshanponnappa opened this Issue Apr 12, 2015 · 7 comments

Comments

Projects
None yet
3 participants
@roshanponnappa

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('9km
3km')
Note: i understand multiply/divide of units are not supported, however parse should work accurately!

@FSMaxB

This comment has been minimized.

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"
        }
      ]
    }
  ]
}
@josdejong

This comment has been minimized.

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

This comment has been minimized.

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.

@josdejong

This comment has been minimized.

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

This comment has been minimized.

roshanponnappa commented Apr 18, 2015

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

@roshanponnappa roshanponnappa changed the title from Incorrect parse with units and multiply/divide to Incorrect evaluate with units and multiply/divide Apr 18, 2015

@josdejong

This comment has been minimized.

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 ?

@josdejong josdejong closed this Sep 21, 2015

@roshanponnappa

This comment has been minimized.

roshanponnappa commented Sep 21, 2015

Thanks! @josdejong

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment