mossadal/math-parser

Negative numbers on the right hand side of an operator throw a Syntax Error

Closed this issue · 4 comments

If you use a negative number on the right hand side, the StdMathParser will return a Syntax Error.

For example: 6 + -1 which is valid and would result in 5.

If you use a negative number on the right hand side, the StdMathParser will return a Syntax Error.

For example: 6 + -1 which is valid and would result in 5.

use (6) + (-1)

I have written a piece of code that wraps the -1 on the right hand side to (-1), resulting in 6 + (-1), which solves the issue for me right now, but it'd be nice to see it fixed.

I use brackets for each number and the calculation is correct. For example: (x) / (y) + (-z) + (2*x)

Thank you for the report. In my opinion, this behavior is a feature, not a bug (and is by design).

I realize that PHP itself allows constructions such as 2+-1, but for my own main use case (parsing student input), this notation is bad form and I am not going to change how StdMathParser acts. Feel free to create a pull request with an alternative parser that handles these expressions.