Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Basic maths - operator precedence


Basic maths - operator precedence

Author
Message
Raghavendra Mudugal
Raghavendra Mudugal
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1784 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
Dineshbabu
Ten Centuries
Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)Ten Centuries (1.1K reputation)

Group: General Forum Members
Points: 1110 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 OoTD

Statement 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 =>540
36/4 =>9

Now the expression will be "SELECT (540- 9) "

so final result is 531

--
Dineshbabu
Desire to learn new things..
sipas
sipas
Say Hey Kid
Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)

Group: General Forum Members
Points: 687 Visits: 706
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 / 6
Divide and Multiply (DM) before Add and Subtract (AS), so
12 + 24 / 6
12 + 4
16

Within 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 8
12 + 0.5 x 8
12 + 4
16

But 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
Raghavendra Mudugal
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1784 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 OoTD

Statement 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 =>540
36/4 =>9

Now the expression will be "SELECT (540- 9) "

so final result is 531


thanks, 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 / 4

SELECT 45 * 9 - 5 + 8 - 9

SELECT 405 - 5 + 8 - 9

SELECT 405 - 6

answer = 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
Raghavendra Mudugal
SSCommitted
SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)SSCommitted (1.8K reputation)

Group: General Forum Members
Points: 1784 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 / 6
Divide and Multiply (DM) before Add and Subtract (AS), so
12 + 24 / 6
12 + 4
16

Within 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 8
12 + 0.5 x 8
12 + 4
16

But 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.
TomThomson
TomThomson
SSChampion
SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)SSChampion (11K reputation)

Group: General Forum Members
Points: 11489 Visits: 12092
bitbucket-25253 (2/5/2013)
Not much different from this one
http://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
mtassin
SSCarpal Tunnel
SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)SSCarpal Tunnel (4.3K reputation)

Group: General Forum Members
Points: 4323 Visits: 72513
Thanks for an easy Order of Operations question.



--Mark Tassin
MCITP - SQL Server DBA
Proud member of the Anti-RBAR alliance.
For help with Performance click this link
For tips on how to post your problems
sknox
sknox
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2161 Visits: 2755
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 = 5
2 + 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, take

8 / 4 x 2

The correct way is left-to-right:

8 / 4 = 2 x 2 = 4

If 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) = 1

It's the difference between

8
-- x 2
4

and

8
-------
4 x 2

Edit: clarified a bit - and then corrected the spelling on the clarification.
sipas
sipas
Say Hey Kid
Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)Say Hey Kid (687 reputation)

Group: General Forum Members
Points: 687 Visits: 706
sknox (2/6/2013)
Always work left-to-right -- don't give precedence to an operation on the same level.
For example, take

8 / 4 x 2

The correct way is left-to-right:

8 / 4 = 2 x 2 = 4

If 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) = 1

It's the difference between

8
-- x 2
4

and

8
-------
4 x 2


I 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 2
is the same as
8 x 2 / 4
or
2 / 4 x 8

Calculating 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 / 10
is the same as
8 x 5 / 2 x 3 / 10 x 2 / 4
is the same as
5 / 2 x 8 x 3 / 4 / 10 x 2
sknox
sknox
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2161 Visits: 2755
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, take

8 / 4 x 2

The correct way is left-to-right:

8 / 4 = 2 x 2 = 4

If 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) = 1

It's the difference between

8
-- x 2
4

and

8
-------
4 x 2


I 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 2
is the same as
8 x 2 / 4
or
2 / 4 x 8

Calculating 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 / 10
is the same as
8 x 5 / 2 x 3 / 10 x 2 / 4
is the same as
5 / 2 x 8 x 3 / 4 / 10 x 2


To a computer, there are no intrinsic numerators and denominators in the statement

8 / 4 x 2

There 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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search