## Basic maths - operator precedence

 Author Message Raghavendra Mudugal SSCarpal Tunnel Group: General Forum Members Points: 4342 Visits: 2958 sipas (2/6/2013)Toreador (2/6/2013)archie flockhart (2/6/2013)It can matter whather you do the multiply or divide first. (12 / 3) * 2 will give a different answer to 12 / (3*2)Yes, but that's covered by the "B" in BODMAS - brackets come first.Yes, but to clarify, if there are no brackets you have to start with the number on the left, not at some random point in the middle. After that, it makes no difference if you multiply or divide first, i.e. you can do the 12 x 2 and then divide by 3, or do the 12 / 3 and then multiply by 2. But you can't do the 3 x 2 and then divide 12 by the result.so, if no brackets then just start from L to R no matter what the operator is... and in the above given equation we don't know where to place the brackets and it leads to follow traditional BODMAS (excluding the BO in the sequence)... correct ? ww; Raghu--The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart. Dineshbabu SSCommitted Group: General Forum Members Points: 1610 Visits: 569 Raghavendra Mudugal (2/6/2013)so, if no brackets then just start from L to R no matter what the operator is... and in the above given equation we don't know where to place the brackets and it leads to follow traditional BODMAS (excluding the BO in the sequence)... correct ?Let's take our today's OoTDStatement A:SELECT (45 * (9 - 5 + 8)- 36 / 4)Brackets first. If we have nested brackets ,then inner most first.. So first we take (9 - 5 + 8) Addition and subtraction have same precedence. In that case go from Left to Right. (9 - 5 + 8) => (4+8) (4+8) =>12 so the exprresion will look like "SELECT (45 * 12- 36 / 4) "Division and Multiplication has higher precedence over Addition and subtraction. So first Multiplication and divsion has to be done before + or -.45*12 =>54036/4 =>9Now the expression will be "SELECT (540- 9) "so final result is 531 --DineshbabuDesire to learn new things.. sipas Ten Centuries Group: General Forum Members Points: 1275 Visits: 751 Raghavendra Mudugal (2/6/2013)so, if no brackets then just start from L to R no matter what the operator is... Almost... If there are no brackets, use BODMAS to work out the order, and work from left to right within each category, e.g.:12 + 3 x 8 / 6Divide and Multiply (DM) before Add and Subtract (AS), so12 + 24 / 612 + 416Within the Divide and Multiply section, you could do the divide before the multiply to give the same result:12 + 3 x 8 / 6 = 12 + 3 / 6 x 812 + 0.5 x 812 + 416But it's simpler to just work from left to right within each BODMAS category, and better to remove all ambiguity with brackets anyway. Raghavendra Mudugal SSCarpal Tunnel Group: General Forum Members Points: 4342 Visits: 2958 Dineshbabu (2/6/2013)Raghavendra Mudugal (2/6/2013)so, if no brackets then just start from L to R no matter what the operator is... and in the above given equation we don't know where to place the brackets and it leads to follow traditional BODMAS (excluding the BO in the sequence)... correct ?Let's take our today's OoTDStatement A:SELECT (45 * (9 - 5 + 8)- 36 / 4)Brackets first. If we have nested brackets ,then inner most first.. So first we take (9 - 5 + 8) Addition and subtraction have same precedence. In that case go from Left to Right. (9 - 5 + 8) => (4+8) (4+8) =>12 so the exprresion will look like "SELECT (45 * 12- 36 / 4) "Division and Multiplication has higher precedence over Addition and subtraction. So first Multiplication and divsion has to be done before + or -.45*12 =>54036/4 =>9Now the expression will be "SELECT (540- 9) "so final result is 531thanks, i have tried that in the morning and I got that, but my question (in my previous reply) was not using any brackets, so BODMAS concept (excluding BO) would apply with L to R move... i guess. (and was trying this below and I kind of answered my self...)`SELECT 45 * 9 - 5 + 8- 36 / 4SELECT 45 * 9 - 5 + 8 - 9SELECT 405 - 5 + 8 - 9SELECT 405 - 6answer = 399` ww; Raghu--The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart. Raghavendra Mudugal SSCarpal Tunnel Group: General Forum Members Points: 4342 Visits: 2958 sipas (2/6/2013)Raghavendra Mudugal (2/6/2013)so, if no brackets then just start from L to R no matter what the operator is... Almost... If there are no brackets, use BODMAS to work out the order, and work from left to right within each category, e.g.:12 + 3 x 8 / 6Divide and Multiply (DM) before Add and Subtract (AS), so12 + 24 / 612 + 416Within the Divide and Multiply section, you could do the divide before the multiply to give the same result:12 + 3 x 8 / 6 = 12 + 3 / 6 x 812 + 0.5 x 812 + 416But it's simpler to just work from left to right within each BODMAS category, and better to remove all ambiguity with brackets anyway.that helps... thank you.. ww; Raghu--The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart. Tom Thomson SSC-Dedicated Group: General Forum Members Points: 36075 Visits: 12792 bitbucket-25253 (2/5/2013)Not much different from this onehttp://www.sqlservercentral.com/questions/Precedence/95143/It must be effectively quite a bit different, although it is similar: the older question only got 30 (under 2%) wrong answers to date. Today's question already has 124 wrong answers (24%) out of less that a third of the number of replies. The results of the earlier question could have made us complacent about the abilities of DBAs, the results of this one should eliminate any such complacency. Tom mtassin SSCrazy Eights Group: General Forum Members Points: 9900 Visits: 72521 Thanks for an easy Order of Operations question. --Mark Tassin MCITP - SQL Server DBAProud member of the Anti-RBAR alliance.For help with Performance click this linkFor tips on how to post your problems sknox SSCertifiable Group: General Forum Members Points: 5287 Visits: 3003 sipas (2/6/2013)BODMAS says:B Brackets first O Orders (i.e. Powers and Square Roots, etc.) DM Division and Multiplication (left-to-right) AS Addition and Subtraction (left-to-right) It actually doesn't matter whether you do the division or multiplication first, but it's easier to keep track if you work left to right.So:2 + 5 x 6 / 10 = 2 + 30 / 10 = 2 + 3 = 52 + 5 / 10 x 6 = 2 + 0.5 x 6 = 2 + 3 = 5 Of course you should always remove any possible ambiguity by using brackets to make it clear what your intention is.Actually it does matter. Within a given precedence level, always work left-to-right -- don't give precedence to an operation on the same level.For example, take8 / 4 x 2The correct way is left-to-right:8 / 4 = 2 x 2 = 4If we do division first, we get the same answer, as it's still left-to-right. But if we do multiplication first, we get:8 / (4 x 2 = 8) = 1It's the difference between8-- x 24and8-------4 x 2Edit: clarified a bit - and then corrected the spelling on the clarification. sipas Ten Centuries Group: General Forum Members Points: 1275 Visits: 751 sknox (2/6/2013)Always work left-to-right -- don't give precedence to an operation on the same level.For example, take8 / 4 x 2The correct way is left-to-right:8 / 4 = 2 x 2 = 4If we do division first, we get the same answer, as it's still left-to-right. But if we do multiplication first, we get:8 / (4 x 2 = 8) = 1It's the difference between8-- x 24and8-------4 x 2I agree you should always work from left to right for the avoidance of doubt. But (as long as you don't add brackets in your head), the order you carry out multiplication and division doesn't actually make a difference to the result. In your example:8 / 4 x 2is the same as8 x 2 / 4or2 / 4 x 8Calculating the 4 x 2 part first is invalid because you are changing the 2 from a numerator to a denominator. It's not the order in which you carry out the operations that matters, as long as you carry out the correct operations!8 x 2 / 4 x 3 / 2 x 5 / 10is the same as8 x 5 / 2 x 3 / 10 x 2 / 4is the same as5 / 2 x 8 x 3 / 4 / 10 x 2 sknox SSCertifiable Group: General Forum Members Points: 5287 Visits: 3003 sipas (2/6/2013)sknox (2/6/2013)Always work left-to-right -- don't give precedence to an operation on the same level.For example, take8 / 4 x 2The correct way is left-to-right:8 / 4 = 2 x 2 = 4If we do division first, we get the same answer, as it's still left-to-right. But if we do multiplication first, we get:8 / (4 x 2 = 8) = 1It's the difference between8-- x 24and8-------4 x 2I agree you should always work from left to right for the avoidance of doubt. But (as long as you don't add brackets in your head), the order you carry out multiplication and division doesn't actually make a difference to the result. In your example:8 / 4 x 2is the same as8 x 2 / 4or2 / 4 x 8Calculating the 4 x 2 part first is invalid because you are changing the 2 from a numerator to a denominator. It's not the order in which you carry out the operations that matters, as long as you carry out the correct operations!8 x 2 / 4 x 3 / 2 x 5 / 10is the same as8 x 5 / 2 x 3 / 10 x 2 / 4is the same as5 / 2 x 8 x 3 / 4 / 10 x 2To a computer, there are no intrinsic numerators and denominators in the statement8 / 4 x 2There are only operators and operands. If you choose the multiplication operator first, the available valid operands are 4 and 2.You are converting the / division operator into a fraction bar in your head and hence converting the division operation into a fractional operand, vis:` 2 3 5 8 x -- x -- x -- 4 2 10`This is common in straight mathematics, because they are mathematically equivalent. But a computer has to do an operation at a time, and treats division as an operation rather than as the creation of a fractional operand.