-
Notifications
You must be signed in to change notification settings - Fork 34
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
Order of operations between powers and multiplications #21
Comments
In these cases, you must use the mathematical association operator. It would look like this: $evaluator = new \Matex\Evaluator();
$evaluator->execute('4*(3^2)'); // returns 36
$evaluator->execute('45/(3^2)'); // returns 5 |
This would indeed return the correct result but it does not really solve my problem. My app uses randomly generated math expressions (which are later displayed in Latex format) so adding parenthesis where necessary would be quite a painful process. |
I think this is a pretty fundamental flaw in an expression evaluator, and agree implementing it in the package itself rather than working around it would be preferable. I'm taking a look at the source trying to understand where the order of operations is defined, but I'm having a hard time. |
If you need something much more complete to work with complex and dynamic expressions, I advise you to use this symfony component: Expression Language https://gist.github.com/mccarlosen/ddf4b923da2a49442ae2344b850e282c |
Thank you, that is for sure a very viable alternative, but doesn't change the fact that this is a mathematical expression evaluator, and as such it should be able to evaluate according to the standard order of operations. |
Describe the bug
The order of operations between powers and multiplications is not what I expect
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Following usual PEDMAS order of operations, powers should be computed before multiplications and/or divisions.
The above code should then return 36 on line 2 and 5 on line 3
The text was updated successfully, but these errors were encountered: