# Basic maths - operator precedence

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

Exactly! I wasn't interested in the product of 45*12 either, only in that three of the four used it and that statement B substituted 45*(-4). This would have been a bit more difficult if the elements had used variables instead of constants. Something like:

Which of the following queries is not guaranteed to provide the same answer?

Statement A. SELECT (A * (B - C + D) - E / F)

Statement B. SELECT (A * (B - (C + D)) - E / F)

Statement C. SELECT (A* (B - C + D) - (E / F))

Statement D . SELECT (A * ((B - C) + D) - E / F)

(Now I sit back and wait for someone to say something about how if A is a tinyint.....) 😉

• Good question, Paul. Since my early days when learning BASIC I memorized the BOMDAS L-R precedence order.

For whoever knows, if you can confirm, I think there is a programming language/environment that assigns a higher precedence to square brackets ("[]") than to round brackets ("()"), can anyone confirm this?

"El" Jerry.

"A watt of Ottawa" - Gerardo Galvan

• All you need to know to answer this question is that parenthesis override any operator precedence. The answer is easily arrived at by finding the odd man out among the four. After getting it right,

in the spirit of Qotd, I still went back to BOL to remind myself of the arith operator precedence.. Thanks for the question 😀

___________________________________________________________________
If I can answer a question then anyone can answer it..trying to reverse the logic.. :hehe:

• Thanks for the question Paul.

• Thanks guys for your wishes. I knew there will be some brainstroming. This actually I came into while teaching my son some maths. His results were different than the one I got it in SQL.:-)

• venkat9.sql (2/6/2013)

All you need to know to answer this question is that parenthesis override any operator precedence.

Parentheses are part of the operator precedence 😉

• Great question, but not so easy. It's almost too early in the morning to be doing math. :hehe:

Everything is awesome!

venkat9.sql (2/6/2013)

All you need to know to answer this question is that parenthesis override any operator precedence.

Parentheses are part of the operator precedence 😉

Warning: this is somewhat off topic, but I think that statement is sufficiently wrong that I can properly go off topic to challenge it.

Not unless bracket pairs are operators, in which case you are talking about operators that (a) are distributed as opposed to unitary (the brackets occupy two positions in a string, one for the opening and another for the close, and the matching rules have to be observed), (b) operate on the semantics of an expression rather than working on the values of arguments, and (c) have somewhat strange scope rules (I think - as I'm not aware of any attempt to define brackets that way it's possible that there's some way of avoiding the complicated scope rules that I haven't noticed. I'm not aware of any mathematical language that treats brackets that way. It's far easier to have a BNF definition of expressions that defines (using sematic marking additions to basic BNF) how the brackets override operator precedence and the left to right rule in an obvious manner. At least that's what I thought back in 1968 - I'm hopelessly out of date on this sort of thing, so maybe I should not still think it (although I do).

Actually, Polish notation (whether reverse or forward) makes life much easier than infix notation (the style used in T-SQL and most other computer languages) - there's no need at all for brackets or even for operator precedence. There are a few computer languages that use reverse Polish notation for expressions and some (a smaller few, I think) that use forward Polish, and when I look at the debate on this QotD I'm tempted to say that it's unfortunate that school arithmetic and algebra and all computer languages don't use (reverse?) Polish notation.

edit: I find it pretty easy to resist that temptation, because that would also require all computer languages to have a sufficiently strong type system to make Polish notation unambiguous in the small scale rather than requiring the whole of an expression to be parsed before one knows whether any tentative parsing so far is correct, and this screws up some forms of polymorphism (maybe undesirable forms, but maybe not).

Tom

• Just come across this qotd and bizarrely I was helping my Daughter out with order of operator questions last night for her school homework - go figure - would have looked a right plank if I'd have got it wrong!! 😀

MCITP

Try not! Do or do not, there is no try

email: info@weekendwebdesign.co.uk
Personal Website: http://markallen.co.uk/

• I taught English for a while. This is not a criticism but information. The word "math" is both plural and singular. If you use the word math is a plural sense, it is already a plural. This is much the same as if the word "you", also both singular and plural, were to be used in the plural by saying "yous", meaning you and you and you. So, the word math, should never have an "s" on the end unless it is somehow used to show possession such as "This is one of math's traits". In this particular case, it is always "apostrophe s". Again, the word "maths" does not exist.

Jamie

• Jamie Longstreet-481950 (2/24/2013)

I taught English for a while. This is not a criticism but information. The word "math" is both plural and singular. If you use the word math is a plural sense, it is already a plural. This is much the same as if the word "you", also both singular and plural, were to be used in the plural by saying "yous", meaning you and you and you. So, the word math, should never have an "s" on the end unless it is somehow used to show possession such as "This is one of math's traits". In this particular case, it is always "apostrophe s". Again, the word "maths" does not exist.

I would guess, Jamie, that you taught American English. As an American, I too was slightly thrown by the use of "maths" as a contraction of "mathematics", but have since found that it is a common and accepted usage in the UK and other parts of the world such as India that are more strongly connected with British English than the American variety.

• john.arnott (2/24/2013)

Jamie Longstreet-481950 (2/24/2013)

I taught English for a while. This is not a criticism but information. The word "math" is both plural and singular. If you use the word math is a plural sense, it is already a plural. This is much the same as if the word "you", also both singular and plural, were to be used in the plural by saying "yous", meaning you and you and you. So, the word math, should never have an "s" on the end unless it is somehow used to show possession such as "This is one of math's traits". In this particular case, it is always "apostrophe s". Again, the word "maths" does not exist.

I would guess, Jamie, that you taught American English. As an American, I too was slightly thrown by the use of "maths" as a contraction of "mathematics", but have since found that it is a common and accepted usage in the UK and other parts of the world such as India that are more strongly connected with British English than the American variety.

You are too kind John. There must be very few people, even in the USA, who have not noticed that "maths" is a singular form, not a plural of math but an alternative word with the same meaning. I would say that jamie's post is the sort of over-pedantic drivel that has given such a terrible reputation to teachers of the English language, on both sides of the pond. It's almost as inane as the claims that in English you can't end a clause with a preposition (a claim that demonstrates a total inability to recognise the existence of "phrasal verbs", or of any other construct that can lead to a stranded preposition, originating with John Dryden as an attack on Ben Johnson - Dryden provided no justification for his bizarre proposition) or split an infinitive (which has been done by many well known authors for something more than 700 years).

I often wonder why it is that English seems to attract so much more of this nonsense than other languages.

Or maybe Jamie was joking (or gently trolling - in which case he certainly caught me)?

Tom

• May have something to do with a sense of pride. I was raised to think there was a difference between proper and improper. I was not trolling and I was not joking.

Jamie

• Jamie Longstreet-481950 (2/25/2013)

May have something to do with a sense of pride. I was raised to think there was a difference between proper and improper. I was not trolling and I was not joking.

.. but not raised to understand that there are differences between varieties of English ?

"Math" grates on my ear and sounds unnatural to me. We'd never use the term here in the UK. "Maths" is the abbreviated form of the name of the field of study in which I qualified long ago. But I recognise that "math" is the term used in American English and wouldn't call anyone out for "improper" usage.

I also abbreviate "statistics" as "stats", which is surprisingly consistent for the English language.

Any language that can have (at least) seven different pronunciations for -ough- can tolerate the odd variation in whether we have -s at the end of an abbreviation.

"Although he came through the borough of Slough, we thought his home was in rough country near Lough Earn ..."

• archie flockhart (2/26/2013)

.....

"Although he came through the borough of Slough, we thought his home was in rough country near Lough Earn ..."