Skip to content
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

Open
daniel-cabane opened this issue Feb 8, 2021 · 5 comments
Open

Order of operations between powers and multiplications #21

daniel-cabane opened this issue Feb 8, 2021 · 5 comments

Comments

@daniel-cabane
Copy link

Describe the bug
The order of operations between powers and multiplications is not what I expect

To Reproduce
Steps to reproduce the behavior:

$evaluator = new \Matex\Evaluator();
$evaluator->execute(4*3^2); // returns 144
$evaluator->execute(45/3^2); // returns 225

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

@mccarlosen
Copy link

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

@daniel-cabane
Copy link
Author

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.
Besides, PEDMAS is the most commonly accepted convention for the order of operation (as far as I know). I think it would be to this library's benefit to follow it.

@ghost
Copy link

ghost commented Dec 15, 2021

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.

@mccarlosen
Copy link

mccarlosen commented Dec 15, 2021

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

@ghost
Copy link

ghost commented Dec 21, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants