• HereIAm@lemmy.world
    link
    fedilink
    English
    arrow-up
    3
    arrow-down
    1
    ·
    edit-2
    3 days ago

    Except it does matter. I left some examples for another post with multiplication and division, I’ll give you some addition and subtraction to see order matter with those operations as well.

    Let’s take:
    1 + 2 - 3 + 4

    Addition first:
    (1 + 2) - (3 + 4)
    3 - 7 = -4

    Subtraction first:
    1 + (2 - 3) + 4
    1 + (-1) + 4 = 4

    Right to left:
    1 + (2 - (3 + 4))
    1 + (2 - 7)
    1 + (-5) = -4

    Left to right:
    ((1 + 2) - 3) + 4
    (3 - 3) + 4 = 4

    Edit: You can argue that, for example, the addition first could be (1 + 2) + (-3 + 4) in which case it does end up as 4, but in my opinion that’s another ambiguous case.

    • troistigrestristes@lemmy.eco.br
      link
      fedilink
      English
      arrow-up
      1
      ·
      2 days ago

      Oh, but of course the statement changes if you add parentheses. Basically, you’re changing the effective numbers that are being used, because the parentheses act as containers with a given value (you even showed the effective numbers in your examples).

      Get this : + 1 - 1 + 1 - 1 + 1 - 1 + 1

      You can change the result several times by choosing where you want to put the parentheses. However, the order of operations of same priority inside a container (parentheses) does not change the resulting value of the container.

      In the example, there were no parentheses, so no ambiguity (there wouldn’t be any ambiguity with parentheses either, the correct way of calculating would just change), and I don’t think you can add “ambiguity” by adding parentheses — you’re just changing the effective expression to be evaluated.

      By the way, this is the reason why I absolutely overuse parentheses in my engineering code. It can be redundant, but at least I am SURE that it is going to follow the order that I wanted.