The correct answer should be 4, because it should have done it in this order:
(5 -2) + 1.
Instead of this:
5 - (2 +1).
Previously this was chalked up to having the order of operations wrong such that all addition was being done before all subtraction. However that is not a sufficient explanation for the problem because expressions are also evaluating right-to-left when there are no addition operations and all the operations are subtractions This can be proved with this example:
print 5 - 2 - 1.
4
The correct answer is 2, because it should be this:
(5 -2) - 1.
But it's giving 4 because it's doing this:
5 - (2 - 1).
This was behind one of the bugs I was trying to figure out in one of my scripts. Now that I know that it's doing this I can sprinkle in lots of extra parentheses to force the order of operations to go left-to-right, but it would be nice not to have to clutter up the script with that.
The correct answer should be 4, because it should have done it in this order: (5 -2) + 1. Instead of this: 5 - (2 +1).
Previously this was chalked up to having the order of operations wrong such that all addition was being done before all subtraction. However that is not a sufficient explanation for the problem because expressions are also evaluating right-to-left when there are no addition operations and all the operations are subtractions This can be proved with this example:
The correct answer is 2, because it should be this: (5 -2) - 1. But it's giving 4 because it's doing this: 5 - (2 - 1).
This was behind one of the bugs I was trying to figure out in one of my scripts. Now that I know that it's doing this I can sprinkle in lots of extra parentheses to force the order of operations to go left-to-right, but it would be nice not to have to clutter up the script with that.