OT: Arithmetic order of operations

Oct 21, 2024 Last reply: 1 year ago 59 Replies

One of those silly "what's the right answer" questions that is circulating on Facebook.


12(2+3)/6(2+3)

where "/" is the division sign.



Clearly by BODMAS, you evaluate the brackets first, so:


12 x 5 / 6 x 5

But...



Do you evaluate that as



(12 x 5) / (6 x 5) = 2 (multiplication before division)



or as


12 x (5/6) x 5 = 50 (division before multiplication)


(where "x" is the multiplication sign)



The latter is also what you would get if you evaluated strictly left to right:



((12 x 5) / 6) x 5


If the expression is correctly drawn, there will be no ambiguity. The division line will be drawn above the divisor, so either

12(2+3) 12(2+3) (2+3)

----------- or ------ (2+3) or 12 ------- (2+3) 6(2+3) 6 6

A compiler will generally parse an expression into Reverse Polish Notation and evaluate it that way, so probably the last in this case. People doing this sort of thing for real should be using enough brackets, which cost little, to remove any possible doubt about the intention. Kill the bugs *before* they are hatched...

Everything inside brackets first.

I don't think there is a right answer.

A mathematician would not write an expression in that ambiguous way, so it's really a question of how an expression in a computer language should be evaluated, and that depends on the specification of the language.

nib

the "DM" in BODMAS say Division before multiplication so this is strictly correct.

See above

Correct, these questions are all simply "click bait" framed to be ambiguous and designed to drive engagement and so increase a pages ratings.

Dave

I think you’re missing the point that these stupid quizzes have nothing to do with there being a “right” answer but are everything to do with harvesting the names of folk who respond to these things.

Tim

Exactly, so the correct approach is to look up the donkeymentation for the language in question and follow whatever they have for operator precedence, using parens as required to get it to do what you meant.

Where I used to work we had a batch of pocket calculators,for a specifc use, set up using reverse polish. Much to the consternation of anyone thinking they were "normal" calculators, each one they used gave exactly the same wrong answers .

The correct answer is to use parenthesis and write it the same way in every language. F*ck documentation.

I can't think of a language that allows implicit multiplication 12(2+3) as opposed to 12*(2+3)?

As a mathematician, I would have assumed implicit multiplication had higher precedence than division. I had no idea if that was correct, so I just looked it up, and the first Google hit agreed with me. There is something called implicit multiplication and that it had higher precedence than explicit /, so I'm going to quit looking further while I'm ahead (I know the real correct answer is use parenthesis)

Mathematicians tend to abuse notation much more often than programmers.

Possibly not actually harvesting your details but exploiting the algorithms social media platforms use to direct more of the s*it towards you. Respond to one of these questions and you will find more of the same being randomly offered to you in the way of content. The originator of the question/content is often paid on the number of hits and the associated advertising which increase the more the social media algorithm targets subsequent postings to people who have previously showed an interest in this type of content.

I am glad you said that. something in my brain automatically did the implicits first but I couldn't remember why.

PARENTHESES - not brackets - are an essential feature of computer processing of algebraic expressions. The first-encountered close parenthesis in the (remaining) expression is the end point of each parser pass. This explains zero-parameter terms, e.g. excel's =TIME()

No need for parentheses in RP. Anyone remember the programming language BURP? Basic Using Reverse Polish.

The US uses PEMDAS.

In most programming languages there is an expectation that brackets are used wherever the order can be misinterpreted.

MISRA is an example of a set of rules to prevent ambiguity.

No! Somebody once failed to teach me ALGOL 68!

I was taught that!

I used it for programs to calculate amateur-radio satellite orbits.

I had a play with Forth long ago, and my first calculator used RPN.

FORTH, like PERL, is a write-only language.

I did get that impression, compact and not exactly self-documenting until you get quite a bit of practice.

I wrote a histogram package in assembler once, as almost my first serious bit of programming (this was about 1970). That teaches you discipline and how important it is not to write spaghetti.It also teaches you to avoid GOTO.

Since then, my view has been that the purpose of programming languages is to make it easy to write assembler. So the constructs that you get in BCPL, C, PHP, JavaScript and similar are what count, along with ensuring the machine has a stack pointer. Thus you also get recursion and re-entrancy for nothing along with structures. Fine so far, until one gets a GUI and has to interact with *that*.

Thus I see FORTH as pointless.

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required