• Omega_Jimes
    link
    fedilink
    arrow-up
    12
    ·
    7 months ago

    I’ve never had anyone tell me operations with the same priority can be done either way, it’s always been left to right.

    • nutcase2690@lemmy.dbzer0.com
      link
      fedilink
      arrow-up
      4
      ·
      7 months ago

      I’ve always heard it that way too but I think it is for consistency with students, imo Logically, if you are looking at division = multiplying by inverse and subtraction = adding the negative, you should be able to do it both ways. Addition and multiplication are both associative, so we can do 1+2+3 = (1+2)+3 = 1+(2+3) and get the same answer.

      • ReveredOxygen@sh.itjust.works
        link
        fedilink
        English
        arrow-up
        1
        ·
        7 months ago

        But subtraction and division are not associative. Any time you work on paper, 2 - 2 - 2 would equal -2. That is, (2-2)-2=0-2=-2. If you evaluate right to left, you get 2-2-2=2-(2-2)=2-0=2

        • nutcase2690@lemmy.dbzer0.com
          link
          fedilink
          arrow-up
          3
          ·
          7 months ago

          Correct, subtraction and division are not associative. However, what is subtraction if not adding the opposite of a number? Or division if not multiplying the inverse? And addition and multiplication are associative.

          2-2-2 can be written as 2 + (-2) + (-2) which would equal -2 no matter if you solve left to right, or right to left.

          In your example with the formula from right to left, distributing the negative sign reveals that the base equation was changed, so it makes sense that you saw a different answer.

          2 - (2 - 2) = 2 + ((-2) + 2) = 2

    • I’ve never had anyone tell me operations with the same priority can be done either way, it’s always been left to right

      It’s left to right within each operator. You can do multiplication first and division next, or the other way around, as long as you do each operator left to right. Having said that, you also can do the whole group of equal precedence operators left to right - because you’re still preserving left to right for each of the two operators - so you can do multiplication and division left to right at the same time, because they have equal precedence.

      Having said that, it’s an actual rule for division, but optional for the rest. The actual rule is you have to preserve left-associativity - i.e. a number is associated with the sign to the left of it - and going left to right is an easy way to do that.