• spongebue@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    7 days ago

    x = -i;

    Do many languages let you do that? When it’s in front of a variable I would’ve expected it to be a subtraction operator only and you would need to do x = -1 * i;

    • EvilHankVenture@lemmy.world
      link
      fedilink
      arrow-up
      5
      ·
      7 days ago

      In most languages I’ve seen - is both a unary negation operator and a subtraction operator depending on context. So it would negate an integer literal or a variable in this context.

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

      Why would they not let you do that? I honestly don’t know a single language that wouldn’t let you do that. Same as basic math notation allows you to do that.

      x = -i

      is a totally valid mathematical equation.

      For the downvoters: Find me a single language that supports operators but doesn’t have an unary minus operator

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

        It’s a valid mathematical notation, sure. But there is an implicit understanding that the - in this case is making a number negative rather than subtracting (or, an implicit subtraction from 0).

        With the way negative numbers generally work in binary there would be much different ones and zeroes stored behind the scenes, so handling that would have to be pretty intentional.

        That said, I did just try it in Java because that’s what I work in normally and I swear I had a gotcha with that. But it worked fine as far as I can tell.

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

          Find me a language where it doesn’t work like that, and we’ll continue the discussion.

          Unary minus operator is standard in every single language that I used so far, including C/C++, Java, Python, Kotlin, Lua, JS/TS, Groovy, PHP, Visual Basic, Excel, Mathematica, Haskell, Bash.

          Here’s more info btw: https://en.wikipedia.org/wiki/Unary_operation

      • 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.

    • boonhet@sopuli.xyz
      link
      fedilink
      arrow-up
      1
      ·
      7 days ago

      Personally I would expect it to behave the same in front of a numeric literal and in front of a variable. I do think most languages do that, but I haven’t actually tested that many and could br wrong.

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

        Pretty much all languages do that. It’s a very basic language feature inherited from basic maths notation. Same as x - y subtracts y from x in pretty much any language that supports operators.