you are viewing a single comment's thread
view the rest of the comments
[–] 2 points 11 months ago (16 children)

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;

  • source
  • hideshow 16 child comments
  • [–] 2 points 11 months ago (1 child)

    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.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 11 months ago (4 children)

    I just tested it in PowerShell. Works fine

    $i = 1
    $x = -$i
    $x
    

    Outputs -1

  • source
  • parent
  • hideshow 4 child comments
  • [–] 2 points 10 months ago (3 children)

    Works fine in any language I ever used.

    I'm honestly quite surprised that this very basic language feature is even a matter of discussion here.

  • source
  • parent
  • hideshow 3 child comments
  • [–] 2 points 10 months ago (2 children)

    It certainly makes me question a lot of things. This sub somehow manages to both feed my impostor syndrome and makong me feel like a genius programmer depending on the thread.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 10 months ago* (1 child)

    Totally, yes. I guess there's a ton of non-programmers and total beginners in this community.

    But sometimes there are some crazy good programmers here as well.

    What's really weird though is that I got two downvotes a bit further up for claiming that unary minus is a standard language feature.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 3 points 10 months ago* (last edited 10 months ago) (2 children)

    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

  • source
  • parent
  • hideshow 2 child comments
  • [–] 1 point 10 months ago (1 child)

    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.

  • source
  • parent
  • hideshow 1 child comment
  • [–] 1 point 10 months ago (4 children)

    But of course -- It's just flipping around the -= operator!

  • source
  • parent
  • hideshow 4 child comments
  • [–] 3 points 10 months ago (2 children)

    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.

  • source
  • parent
  • hideshow 2 child comments
  • [–] 2 points 10 months ago (1 child)

    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!

  • source
  • parent
  • hideshow 1 child comment
  • [–] 2 points 10 months 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.

  • source
  • parent