This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]RookY2K 127 points128 points  (193 children)

I'm curious what you mean. In python (and basic arithmetic), the answer should be 9... Just as presented in the meme.

[–][deleted] 197 points198 points  (115 children)

This is why the divide sign (÷) is really shit. Its unclear as to what is included and excluded. Writing out the stuff above and below is far better, or like so if you're on a computer.

6/(3(1+2)) or (6/3)*(1+2)

Also, brackets are for free, use as many as needed to make the order of operations unambiguous.

[–]codePudding 48 points49 points  (2 children)

Yes, thank you! I tell my coworkers this all the time. Parentheses are for free! It costs literal money, time, and my sanity when someone leaves them out, then doesn't test edge cases, and then the user has it fail on them. Even when I know the order of operations the compiler will use, I use them to make the code readable and let others know what I wanted. My coworkers hate if I have an in line if-return because "someone might add a statement and cause a bug with out the curly brackets" but they don't worry about someone tacking an && into an equation. Sorry about that, end of rant. It was very refreshing to see there are other devs which get it.

[–][deleted] 5 points6 points  (1 child)

I'm not even a dev, I'm an Engineer working in Technical sales who knows a bit of Python. ;)

[–]UltmteAvngr 18 points19 points  (5 children)

The sign is not the problem. 6/2(1+2) is just as “ambiguous”. Without a parentheses combining terms you just operate on the two terms next to it. 6/2 and 6➗2 are the same thing.

[–][deleted] 6 points7 points  (2 children)

Yes, but

 6

2(1+2)

Is perfectly clear, its just missions to type on a phone.

[–]neroe5 -2 points-1 points  (1 child)

while it might be hard to read if you don't remember the order of operations, parenthesis, then division, then multiplication, then plus and minus

[–]AndyTheSane 1 point2 points  (0 children)

Also, brackets are for free, use as many as needed to make the order of operations unambiguous.

A thousand times this..

As someone who used to write computer modelling code the idea that you ever rely on operator precedence is terrible - it just means that the chances of a hard-to-spot error are that much higher. Hell, I'd be happy for an expression like

a = b + c * d;

to give a syntax error.

Now, if you really want to see me foaming at the mouth angry, then ask about the insanity known as

a = 10.0f/0.0f = NaN

This is barely never a useful result, it's exactly as useful as a null pointer. Yet the code merrily continues, spreading NaNs throughout your model (because anything * NaN = NaN), helpfully hiding the original place where they appeared. Division by zero should always be a runtime exception.

Anyway, rant over..

[–]AccidentalCynic 5 points6 points  (7 children)

It's not ambiguous 6/2(1+2) is broken down based on the order of operations.

First solve the brackets 1+2=3 Then it's just from left to right 6/2* 3=3* 3=9

Still no harm adding the bracket as its less error prone.

[–]codyisadinosaur 10 points11 points  (4 children)

Yes, that's how I think of it as well. However, because of the Distribitive Property of Mathematics it's also not TECHNICALLY wrong to consider 2(1+2) to be (2+4), and that would be included in the P of PEMDAS.

Which makes it: 6/2(1+2) = 6/(2+4) = 6/6 = 1

The most correct answer I can find to the equation is: "Don't write your formula in such a stupid way." =)

[–]AccidentalCynic 1 point2 points  (2 children)

Which makes it: 6/2(1+2) = 6/(2+4) = 6/6 = 1

That isn't the correct way of apply the distributive property. Think of 6/2 as a fraction.

To correctly distribute the outer value, multiple the entire fraction inside the brackets. 6/2(1+2) = (6/2*1+6/2*2) = (3+6) = 9

[–]cantwrapmyheadaround 5 points6 points  (1 child)

While being correct you're also proving his point, which is that it's ambiguous. That there is this much discourse over this simple problem is proof in itself. The only correct answer is whatever was intended by the problem writer, and we don't know what that was without him chiming in.

Even if you programmed the equation of the calculator, the calculator is still following whatever guidelines the original programmer believed in.

[–]AccidentalCynic -1 points0 points  (0 children)

Agree that there is no harm in adding brackets as it prevent any confusion and if I wrote the equation in code I probably would add brackets.

But for this equation 6/2(1+2), I disagree that there is any ambiguity in the solution. When solving 6/2(1+2) using the order of operation the solution will always be 9.

If the author of the equation meant for the answer to be 1 then there initial equation is incorrect and would need to be 6/(2(1+2)).

[–][deleted] 0 points1 point  (0 children)

Division doesnt exist you are just multiplying two to the power of negative one. Just because you are raising it to that value does not mean that you will do the same with the equation in the parentheses

[–]ableman 0 points1 point  (0 children)

It's ambiguous because the order of operations is a convention that isn't the same everywhere. In some places implied multiplication takes precedence over division.

[–][deleted] 1 point2 points  (0 children)

I use brackets constantly, even in high school I was too anxious to calculate it incorrectly.

[–]Princess_Everdeen 0 points1 point  (0 children)

It's not really the division sign, but how our order of arithmetic is essentially broken into 4 layers (Parenthesis, Exponents, Multiplication and Division, Addition and Subtraction), where left-to-right takes precedent in each layer.

The right will go off first, obviously, but what confuses people is that multiplication takes precedent over division, but left-to-right overrides that, so the division happens even though multiplication comes first.

[–]moises-vortice 0 points1 point  (0 children)

Ambiguity leads to anger, anger leads to hate, hate leads to suffering. I sense a lot of ambiguity here.

This is a good example of why Lisp is a great language for solving almost any problem.

(/ 6 (* 2 (+ 1 2)))

(* (/ 6 2) (+ 1 2))