The Order of Operation | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

The Order of Operation

Many times you find yourself thinking about the mathematical order of operation BODMAS(Brackets,Of,Division,Multiplication,Addition and finally Subtraction) Can the order of operation in mathematical programming be PEDMAS(Pare thesis,Exponential,Division,Multiplication,Addition then Subtraction?

6th Oct 2018, 6:33 AM
Ambeyi A Derrick
Ambeyi A Derrick - avatar
19 Answers
+ 4
Hatsy Rei I know. I was trying to point out how remembering a mnemonic without understanding the underlying idea could lead to incorrect results. I've seem plenty of kids doing that. In your first method you're treating the minus before 2 as the unary negation operator, so 1-2+3=1+(-2)+3. (If we think like that, we don't ever need the subtraction operation.) In your second method, you're using the distributive property in reverse. I've also seen another method being taught, collecting all the numbers with a plus sign before them together at the left, like 1 - 2 + 3 = 1 + 3 - 2 = 4 - 2 = 2 This is the commutative property, of course. But all of these are often difficult to grasp for beginners. Someone could just interpret the rule as "add before subtract" and get incorrect results. And that example is also on Wikipedia, btw: https://en.m.wikipedia.org/wiki/Order_of_operations#Mnemonics But I should have been clearer. So thanks for the clarification, and for explaining the proper ways! 😊
8th Oct 2018, 3:08 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 7
Faraz Shahsavan I went off with a tone I myself didn't quite like, so here is the amended version: This thread is a query in which objective is to obtain the differences between BODMAS and PEMDAS, and why the former is used over the latter. 1) How do you think that your "best answer" clears up the doubt of OP? 2) How is our friendly discussion about associativity and precedence, "not what a user likes to see when searching for answers"? I can still see downvotes on Kishalaya, Ambeyi, and Paulie's answer at the top, so perhaps you would want to shed more light on why these clearly legit and helpful answers are downvoted?
10th Oct 2018, 6:33 AM
Hatsy Rei
Hatsy Rei - avatar
+ 5
Hatsy Rei you may want to see this https://en.m.wikipedia.org/wiki/Operator_associativity here we're talking about the order of operations, so don't think about changing + to - in most cases, operators have left associativity, but the right associativity may not exist, therefore most programming languages compile, or to say "calculate", from left to right when the operators have same priority take 1 - 2 + 3 for example, it's actually (1-2) + 3, because - and + have same priority in python it's represented as this: (1.__add__(2)).__add__(3) and for 1 - 2 * 3, * has higher priority than -, so it's 1 - (2 * 3) or in python: 1.__sub__(2.__mul__(3)) no operators are changeable, just the priorities or having same result as another just as (1 - 2) + 3 has the same result as 1 - (2 - 3)
8th Oct 2018, 3:26 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 4
Yes, but there are minor subtleties regarding interpretation. Consider 2 / 1 * 4 Most languages I've tested produces 8. But if we multiply first, then the result is 0.5. The reason is this: while BODMAS/BEDMAS tells us that D has a higher priority than M, the PEMDAS rule actually considers M and D to be of the same priority (similarly for A and S). If there are multiple such operations, we need to process them left-to-right. So here we'd do 2 / 1 * 4 = 2 * 4 = 8 Which is also the same result if we follow BODMAS/BEDMAS. Now consider another example: 1 - 2 + 3 If someone interprets BODMAS/BEDMAS incorrectly, and decides to do 1 - 2 + 3 = 1 - 5 = -4, they would be obviously wrong. Conclusion: I think in general it's better to consider D and M to have the same precedence, and A and S to have the same, but lower precedence. Then operations of the same precedence need to be resolved left-to-right. And this is exactly the convention most programming languages use.
6th Oct 2018, 8:01 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 4
Kishalaya Saha Trying to raise the explicit answer human! No feelings, hard or soft.
8th Oct 2018, 6:46 AM
Faraz Shahsavan
Faraz Shahsavan - avatar
+ 4
Faraz Shahsavan I have to warn you that downvote abuse is against our guidelines. No feelings, hard or soft.
9th Oct 2018, 8:41 PM
Hatsy Rei
Hatsy Rei - avatar
+ 4
Hatsy Rei , I'm starting to see the points you indicate, however I removed all my downvotes this morning since i read the guidelines and the way things basically work around here. So that one should have been mistaken if I may.
10th Oct 2018, 7:27 AM
Faraz Shahsavan
Faraz Shahsavan - avatar
+ 3
They are all the same rule! In coding, usually the only type of "Brackets" we use in mathematical expressions are Parentheses. BODMAS doesn't include Exponential, as it's probably created for children who are not introduced to it. (Though some interpret the O in BODMAS as Order, which is basically exponential.) "Of" is almost never used in formal mathematical notations and computing, so it's not really needed.
6th Oct 2018, 7:44 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 3
Kishalaya Saha Your second example is flawed though. 1 - 2 + 3 If you were to add first, you would add -2 and 3 to get 1. 1 + 1, which is equal to 2, satisfies the concept that both + and - operators are equivalent in terms of precedence. Even if you were to do 2 + 3, the sign would have been inverted. 1 - (2 - 3) which gives the same answer, 2.
7th Oct 2018, 11:27 PM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Flandre Scarlet I wasn't "changing the operators", just pointing out how BODMAS/PEMDAS is never violated by having operators of equal precedence being evaluated in different order, in the sense that they *can* be right associative, although the norm is otherwise. Kishalaya Saha Yeah, I wasn't truly aware that you were making that example on purpose. There I was wondering if someone would have read that and assume that left associativity is some kind of universal law which must be obeyed because "the rules say so". My bad.
8th Oct 2018, 3:38 AM
Hatsy Rei
Hatsy Rei - avatar
+ 3
Hatsy Rei oh, sorry, obviously i misunderstood what you and Kishalaya Saha are talking about😅
8th Oct 2018, 3:51 AM
Flandre Scarlet
Flandre Scarlet - avatar
+ 3
The regular operators' order is as follows: 1. ( ) [ ] 2. ++ -- 3. * / % 4. + - 5. << >> 6. < > <= >= 7. == != 8. & 9. ^ 10. | 11. && 12. || 13. = += -= *= /=
8th Oct 2018, 6:16 AM
Faraz Shahsavan
Faraz Shahsavan - avatar
+ 3
Faraz Shahsavan , upvoting your own comment is fine! But downvoting almost all the others is not nice! Just saying...
8th Oct 2018, 6:40 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
so its correct to use the PEMDAS formula where the brackets are substituted for parenthesis and addition of exponential
6th Oct 2018, 7:48 AM
Ambeyi A Derrick
Ambeyi A Derrick - avatar
+ 2
I get you now Mr Kishalaya
6th Oct 2018, 8:42 AM
Ambeyi A Derrick
Ambeyi A Derrick - avatar
+ 2
Hatsy Rei , if we think about it, my first example is similarly "flawed". Just replace + by *, - by /, and unary negation by taking the multiplicative inverse. So we'll have think of 2/1*4 as 2*(1^(-1))*4. That whole comment was about interpreting the "rules" properly 😊
8th Oct 2018, 4:02 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 2
Hatsy Rei , I have upvoted posts that include the answer directly. Did not change the posts that did so, indirectly, and downvoted downvoted comments like: Thank you. Since these are not what a user likes to see when seeking answers to their problem. This is far less than even "all the others" as Kishalaya said. And is the type of behavior that leads to better responses. After all you're being quite emotional as you downvote the best and most concise answer I tried to give it and theb use my expression to show it cool and unlike it. No feelings, hard or soft.
10th Oct 2018, 4:22 AM
Faraz Shahsavan
Faraz Shahsavan - avatar
0
the BODMAS rule.
6th Oct 2018, 1:24 PM
Siddhesh