February 6, 2013 at 5:57 am
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.
February 6, 2013 at 6:13 am
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..
February 6, 2013 at 6:16 am
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.
February 6, 2013 at 6:26 am
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.
February 6, 2013 at 6:28 am
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.
February 6, 2013 at 7:08 am
bitbucket-25253 (2/5/2013)
Not much different from this one
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
February 6, 2013 at 7:45 am
Thanks for an easy Order of Operations question.
February 6, 2013 at 7:52 am
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.
February 6, 2013 at 8:15 am
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
February 6, 2013 at 8:41 am
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.
February 6, 2013 at 9:17 am
Good math question, thanks Paul
February 6, 2013 at 9:30 am
First time through I had it wrong, had to reload because it did not look right. Second time through I got it and +1.
Basic, yes. But also one that makes you think about those basics.
Thanks!
Not all gray hairs are Dinosaurs!
February 6, 2013 at 9:56 am
Thanks for the question!
I marked B because I didn't realize the brackets would make any difference in the 9 - 5 + 8 part, since they're all in the same precedence here. Took me a while to realize 9 - (5 + 8) means 9 - 1 * (5 + 8), so both 5 and 8 are considered negative numbers, instead of just 5. That left-to-right rule makes more sense to me, so I learned something new. 🙂
February 6, 2013 at 9:59 am
sknox (2/6/2013)
To a computer, there are no intrinsic numerators and denominators in the statement8 / 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.
Yes, I was only talking 'mathematically'; as you say the computer will carry out one operation at a time using its order of precedence. For this reason, you might want to choose the order in which you present the operations, as even though they're mathematically equivalent, results may vary. For example:
declare @kk numeric;
set @kk = 1;
SELECT (@kk * 6 / 3) as chk
gives 2.000000
but
declare @kk numeric;
set @kk = 1;
SELECT (@kk / 3 * 6) as chk
gives 1.999998
February 6, 2013 at 11:05 am
Really easy one - thanks!
Viewing 15 posts - 31 through 45 (of 59 total)
You must be logged in to reply to this topic. Login to reply