• squaresinger@lemmy.world
      link
      fedilink
      arrow-up
      3
      ·
      6 days ago

      Nope, it is not.

      x = 5
      i = 2
      x -= i // x => 3
      

      while

      x = 5
      i = 2
      x = -i // x => -2
      

      x=-i is the unary minus operator which negates the value right of it. It doesn’t matter if that value is a literal (-3), a variable (-i) or a function (-f()).

      x-=i is short for x = x-i, and here it’s a binary subtraction, so x is set to the result of i subtracted from x.

      • quilan@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        5 days ago

        I need to append /s to my future silly replies I think… that said, I’ll never pooh-pooh a well thought response, so thanks for the nice write-up!

        • squaresinger@lemmy.world
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          5 days ago

          Thanks, I totally missed your sarcasm :)

          There’s a couple people in this threat who seem to actually think that x = -i is some weird magic instead of a standard feature that’s present in every major programming language.

    • spongebue@lemmy.world
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      6 days ago

      That only works if x is already 0

      If i is 10 and x is zero, yes, x -= i would have a value of -10. If x was 5 from something else previously, x-=i would end with an x value of -5.