SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Basic maths - operator precedence


Basic maths - operator precedence

Author
Message
archie flockhart
archie flockhart
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1378 Visits: 1156


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.
kapfundestanley
kapfundestanley
UDP Broadcaster
UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)UDP Broadcaster (1.4K reputation)

Group: General Forum Members
Points: 1448 Visits: 1258
Easy Mathematics.i like it.

“When I hear somebody sigh, ‘Life is hard,’ I am always tempted to ask, ‘Compared to what?’” - Sydney Harris
Raghavendra Mudugal
Raghavendra Mudugal
SSCrazy
SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)SSCrazy (2.5K reputation)

Group: General Forum Members
Points: 2544 Visits: 2958
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.
ldorian81
ldorian81
SSC-Addicted
SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)SSC-Addicted (476 reputation)

Group: General Forum Members
Points: 476 Visits: 325
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.


+1Cool

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.
sipas
sipas
SSC Eights!
SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)

Group: General Forum Members
Points: 840 Visits: 722
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.
archie flockhart
archie flockhart
Ten Centuries
Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)Ten Centuries (1.4K reputation)

Group: General Forum Members
Points: 1378 Visits: 1156


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.
(Bob Brown)
(Bob Brown)
SSC Eights!
SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)SSC Eights! (963 reputation)

Group: General Forum Members
Points: 963 Visits: 1145
Thanks for the easy mid-week question.
Toreador
Toreador
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3144 Visits: 8088
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.
Yogeshwar Phull
Yogeshwar Phull
SSC Eights!
SSC Eights! (952 reputation)SSC Eights! (952 reputation)SSC Eights! (952 reputation)SSC Eights! (952 reputation)SSC Eights! (952 reputation)SSC Eights! (952 reputation)SSC Eights! (952 reputation)SSC Eights! (952 reputation)

Group: General Forum Members
Points: 952 Visits: 680
Thanks for the basics question. +1
sipas
sipas
SSC Eights!
SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)SSC Eights! (840 reputation)

Group: General Forum Members
Points: 840 Visits: 722
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.
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