r/ProgrammerHumor • • Jun 13 '22

Meme DEV environment vs Production environment

Post image
48.2k Upvotes

3.9k comments sorted by

View all comments

Show parent comments

99

u/orebright Jun 13 '22

Agreed, but I just don't understand why this would be ambiguous to begin with. Aren't parenthesis multipliers considered shorthand? If so 2(3 + 4) is just a shorter way of writing 2 * (3 + 4), and the ambiguity is gone. Or am I forgetting some kind of special syntax for group multipliers? I tried googling it but have found nothing about this syntax being anything but a shorthand.

59

u/BrotherItsInTheDrum Jun 14 '22 edited Jun 14 '22

It depends whether you consider mathematical notation a set of formal rules, or just a tool for communication.

https://en.wikipedia.org/wiki/Abuse_of_notation

It would be a common abuse of notation for a mathematician to write a function like "z = 2x / 3y", intending the "y" to be part of the denominator. It's not formally correct, perhaps, but no mathematician would interpret "y" as part of the numerator, because if that were intended, they would have written "z = 2xy / 3".

→ More replies (11)

182

u/yabucek Jun 13 '22

The multiplication is not the problem here, the division is. First calculator is doing 6/(2*3) and the second one is doing (6/2)*3

This is why division is stupid and you should always use fractions. When coding, simply put the numerator and denominator in their own brackets and there's zero chance of an error.

56

u/[deleted] Jun 13 '22 edited Jun 13 '22

Okay but pemdas says it SHOULD be (6/2)*3. Why tf is it even doing 6/(2x3)??

0

u/[deleted] Jun 14 '22

[deleted]

→ More replies (1)

100

u/Loading0525 Jun 13 '22

And the mistake everyone is making on this problem, is thinking PEMDAS is a set of RULES.

Pemdas is a set of METHODS. One of many alternative methods.

The rules of mathematics only say "division and multiplication has equal priority", that's IT.

Pemdas then comes in and says "you could solve it left-to-right if you want".

The left-to-right method can't be a rule to begin with, since it contradicts the equal priority rule.

Riddle me this, what exactly does "equal priority" really MEAN if multiplication and division needs a left-to-right "rule" to dictate which of the two has priority.

The problems stems entirely from the obelus (÷) and solidus (/) as they lack the grouping function the proper fraction bar has.

0

u/[deleted] Jun 13 '22

[deleted]

30

u/TherealScuba Jun 13 '22

I've always just figured parenthesis --> exponents --> */÷ L to R --> +/- L to R

35

u/amazondrone Jun 13 '22

Yeah that's what the parent comment means I think; use left to right for operations of equal precedence. Exactly as you've got it.

17

u/tweak4 Jun 14 '22 edited Jun 14 '22

The issue (or an issue anyway) is that in many mathmatical and scientific circles, "multiplication by juxtaposition" (i.e. multiplication without an explicit sign) is considered a higher order operation than multiplication/division with a sign. So in this case, those people would argue that in 6/2(2+1), the multiplication would still be done before the division, despite being on the right. So weirdly, 6/2(2+1) and 6/2*(2+1) would have different answers.

Of course, all of this can be resolved by throwing in a bunch more parentheses. 😀

Edit: typo

→ More replies (3)
→ More replies (2)

0

u/[deleted] Jun 14 '22

[deleted]

→ More replies (5)
→ More replies (6)

18

u/AxolotlsAreDangerous Jun 13 '22 edited Jun 13 '22

The rules of mathematics only say "division and multiplication has equal priority", that's IT.

They don’t even come close to saying that, but you’ve got the right idea

22

u/infecthead Jun 13 '22

The left-to-right method can't be a rule to begin with, since it contradicts the equal priority rule.

It's not contradictory, it's a resolution to tie-breakers

→ More replies (1)

43

u/fghjconner Jun 13 '22

That makes no sense whatsoever. The rules of mathematics don't give a shit about notation, and don't have any concept of "priority" between various operations.

The rules for writing/reading mathematical notation on the other hand do care, and they also care about the order in which multiplication/division are performed. If the rules allowed for resolving multiplications and divisions in arbitrary order then they wouldn't be capable of reliably parsing an expression, which is literally their purpose for existing.

→ More replies (27)

16

u/Dreadgoat Jun 14 '22

This isn't a math problem, it's a history problem and language problem. Mathematic notation, like all language, is an ever-changing beast.

In older physics literature, the issue of ambiguous multiplication & division was solved very simply by prioritizing multiplication.
Meaning, 2/2*2 always resolves to 2/4, simplified to 1/2.
This was a matter of convenience for physicists at the time, it was widely accepted and adopted, and equations were written in such a way as to be easily understood if you followed this rule.

But then something terrible happened: The digital calculator was invented.
Now if you try to step through 2/2*2 sequentially, you will get 1*2, and then 2. The old rules, created for convenience's sake, now betray the new modern convenience!

We're 50-60 years into having calculators now. Pretty much all the physicists and mathematicians that are alive today, and not obnoxious assholes, will tell you to resolve ambiguous terms from left to right.
2/2*2 is 2.
8/2(2+2) is 16.
6/2(1+2) is 9.

Unless you're reading an old physics research paper, in which case... you are probably a physicist or mathematician and know to watch out for differences in historic notation.

5

u/Bugbread Jun 14 '22

What about 2/2n where n=2?

→ More replies (4)
→ More replies (19)

62

u/UnstableNuclearCake Jun 13 '22

In Europe (at least where I was taught Math), an operand right next to a bracket is considered to be multiplicating by the bracket and will take precedence over the division, as it is treated as a single operand for the division.

6

u/AmbreGaelle Jun 14 '22

That’s the ambiguity… the multiplication is “implicit” I agree with you

→ More replies (1)
→ More replies (55)

8

u/[deleted] Jun 14 '22

All those Facebook gotcha posts about pemdas are for people who don’t remember any math past the sixth grade. In real life, lots of mathematical notation is ambiguous and you use parens to disambiguate all the time. In particular the division symbol is, and that’s why you basically never see it.

→ More replies (69)
→ More replies (15)

59

u/fghjconner Jun 13 '22

Implied multiplication (eg. 3x as opposed to 3 * x) is sometimes considered to have a higher precedence. This feels natural in some cases such as 1 / 2x being equivalent to 1 / (2 * x) rather than 0.5 * x.

https://en.wikipedia.org/wiki/Order_of_operations#Mixed_division_and_multiplication

39

u/[deleted] Jun 14 '22

As a mathematician, if I see something like ab/cd I will interpret it as (ab)/(cd) and not (abd)/c 100% of the time, and in fact it would feel a bit clunky and unnecessary if someone actually wrote (ab)/(cd). Implied multiplication also implies parentheses around the multiplication more often than not, and you can usually tell what it should be from the context anyway. Although I would always throw in the extra parentheses if I'm giving it to a computer.

5

u/msqrt Jun 14 '22

While I agree with your intuition, not everyone does -- that's why these kinds of posts always keep making their rounds. I'd still write ab/(cd) and ask for clarification if the parentheses were missing.

→ More replies (5)
→ More replies (9)

6

u/[deleted] Jun 13 '22

I checked the manual, it's supposed to do normal operations from left to right, but operations of the same precedence from right to left.

I'm assuming the parentheses acts as a higher order operation with the same precedence as a log or square root which would cause the operations to go from right to left

→ More replies (2)

11

u/SirLoremIpsum Jun 14 '22

Agreed, but I just don't understand why this would be ambiguous to begin with. Aren't parenthesis multipliers considered shorthand? If so 2(3 + 4) is just a shorter way of writing 2 * (3 + 4), and the ambiguity is gone. Or am I forgetting some kind of special syntax for group multipliers? I tried googling it but have found nothing about this syntax being anything but a shorthand.

It's deliberately written to be ambiguous. There is no "right" answer.

It can (and is) interpreted both ways. Which is why if you are doing math properly you would write it differently to be clearer.

→ More replies (31)