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

Operator Precedence problem in Shunting Yard Algo #84

Open
Hallett-K opened this issue May 5, 2024 · 1 comment
Open

Operator Precedence problem in Shunting Yard Algo #84

Hallett-K opened this issue May 5, 2024 · 1 comment

Comments

@Hallett-K
Copy link

In the Shunting Yard Algorithm video, we give + a higher precedence than -. Whilst this is usually fine, there's an issue here.

Consider the expression 1 - 2 + 3. Mathematically, if we were to add then subtract, what we want to be doing is -2 + 3. What the code does in this case instead is 2 + 3. This means that we end up doing 1 - 5, rather than 1 + 1, which is incorrect.

The easiest solution would be to give + and - the same precedence, and for the sake of consistency also give * and / the same precedence. Alternatively we'd need to check whether the first parameter is part of a subtraction, which can be a little finnicky.

@quincy451
Copy link

quincy451 commented May 5, 2024 via email

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