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
math_eval() does not handle negative numbers #788
Comments
From the module documentation:
If you really need this feature, I would be more than happy to review a Pull Request. Otherwise, I will put it on the TODO list, but with low prio. |
I understand what your saying, but coming at this from the stance of a user I think it is a problem since it can greatly complicate an algorithm if you cannot pass in a negative number. It is also a problem when you can't feed the output of math_eval back into itself, especially since at the script level these are stored as strings, which can't be converted to an int to check their sign. This is a problem if you would like to keep an intermediate result around, and generally just unintuitive.
I suppose you could do a substr to get the first character and check that way, but now we are doing string operations to deal with a math problem and we have to check the sign after every subtraction. Unless you are going to remove support for subtraction, negative numbers should really be handled. What are you thoughts on using something like TinyExpr from https://github.com/codeplea/tinyexpr ? It seems to handle the above cases well and even opens up for using standard C math functions in the expression as well. It also has test coverage, so it should handle potential corner cases better. It is zlib licensed, which I think is compatible? And it should be pretty easy to include. If you think that looks like a reasonable solution I don't mind writing up a patch to convert the math_eval() function to use it. |
I think it's more than a reasonable solution, especially since it's easy to use (1 file + 1 header). I only wrote our own quick parser because I didn't want to impose too many dependencies, but the fact that
This looks like a limitation - we definitely need to make |
Attempting to use a negative number with math_eval in the mathops module results in a 'Mismatched tokens expression'
Example:
math_eval("10 + (-5)", "$var(test)");
Gives the following errors:
ERROR:mathops:pop_number: RPN Stack Empty
ERROR:mathops:pop_number: RPN Stack Empty
ERROR:mathops:evaluate_rpn_output: Parse expr error: stack has 0 elements
ERROR:mathops:evaluate_exp: Mismatched tokens in expression: <10 + (-5)>
Happens with or without the parentheses.
The text was updated successfully, but these errors were encountered: