Basic maths - operator precedence

  • Good one. 🙂

    (after lahhhhhhhhot of thinking and breaking them bit by bit finally (i think) understood the flow.. but i cheated by executing the code and picking the answer... NOT-:cool:)

    the link provided in the reference section, does that follows the actual BODMAS flow? in the table, the level 2 shows multiply first and then followed by division.. but in the BODMAS division comes first... ? when i broke statement and calculated i ended up with different results.

    (may be i should go through the link again... with eyes open)

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Gary Stacey (2/6/2013)


    I got it wrong!

    I'd applied BODMAS to the 4 statements so I figured it was a different answer. Thought my reasoning was sound, so I checked the link and found:

    When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression.

    You learn something new every day.

    ah... this helps a bit; thank you 🙂

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Hugo Kornelis (2/6/2013)


    You could have saved that time. I just checked for which statements the parentheses matched the order SQL Server would use without parentheses and for which statement a different order was forced. Only one forced a different order, so that had to be the correct answer.

    this is coohuuulll... (Hugo, if you are not busy... then) I need more light here (need to improvise my approach)

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • This was removed by the editor as SPAM

  • Hugo Kornelis (2/6/2013)


    Dineshbabu (2/6/2013)


    But you steal some of my time by making me to sit and calculate each and every statement..:-) .

    You could have saved that time. I just checked for which statements the parentheses matched the order SQL Server would use without parentheses and for which statement a different order was forced. Only one forced a different order, so that had to be the correct answer.

    This shows my lack of experience..:blink:

    --
    Dineshbabu
    Desire to learn new things..

  • the link provided in the reference section, does that follows the actual BODMAS flow? in the table, the level 2 shows multiply first and then followed by division.. but in the BODMAS division comes first... ? when i broke statement and calculated i ended up with different results.

    We learned the mnemonic as "BOMDAS" and also that the Multiply and Divide, and the Add and Subtract, were pairs of items of equal precedence.

    Something like Bo[md][as]

    But I'm always aware that different programming languages, or indeed different programmers reading my code, might interpret expressions differently to me, even if I'm confident that I have them 'right'. So I use brackets extensively to try to avoid any ambiguity.

  • Easy Mathematics.i like it.

    “When I hear somebody sigh, ‘Life is hard,’ I am always tempted to ask, ‘Compared to what?’” - Sydney Harris

  • archie flockhart (2/6/2013)


    We learned the mnemonic as "BOMDAS" and also that the Multiply and Divide, and the Add and Subtract, were pairs of items of equal precedence.

    Something like Bo[md][as] ...

    and in the pair.. say [md] does the m takes precedence or d or just L to R? not sure how it can be equal.

    ww; Raghu
    --
    The first and the hardest SQL statement I have wrote- "select * from customers" - and I was happy and felt smart.

  • Gary Stacey (2/6/2013)


    I got it wrong!

    I'd applied BODMAS to the 4 statements so I figured it was a different answer. Thought my reasoning was sound, so I checked the link and found:

    When two operators in an expression have the same operator precedence level, they are evaluated left to right based on their position in the expression.

    You learn something new every day.

    +1:cool:

    Although I got it right :-P, Nice question tho.:-D

    Regards
    ld

    Stoke-on-Trent
    United Kingdom

    If at first you don't succeed, go to the pub and drink away your current thought plan.

  • 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.

  • 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.

    It can matter whather you do the multiply or divide first.

    (12 / 3) * 2 will give a different answer to 12 / (3*2)

    I'd expect " 12 / 3 * 2" to evaluate left to right, but as I said, I'd always prefer to make it obvious with brackets.

  • Thanks for the easy mid-week question.

  • 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.

  • Thanks for the basics question. +1

  • 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.

Viewing 15 posts - 16 through 30 (of 59 total)

You must be logged in to reply to this topic. Login to reply