• spongebue@lemmy.world
    link
    fedilink
    arrow-up
    2
    ·
    3 months 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
      7
      ·
      3 months 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.

    • boonhet@sopuli.xyz
      link
      fedilink
      arrow-up
      2
      ·
      3 months 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
        ·
        3 months 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.

      • squaresinger@lemmy.world
        link
        fedilink
        arrow-up
        3
        ·
        3 months 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
          ·
          3 months 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
            1
            ·
            edit-2
            3 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.

      • spongebue@lemmy.world
        link
        fedilink
        arrow-up
        1
        ·
        edit-2
        3 months 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.

      • squaresinger@lemmy.world
        link
        fedilink
        arrow-up
        2
        ·
        3 months ago

        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.

        • Sylvartas@lemmy.dbzer0.com
          link
          fedilink
          English
          arrow-up
          2
          ·
          3 months ago

          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.

          • squaresinger@lemmy.world
            link
            fedilink
            arrow-up
            2
            ·
            edit-2
            3 months ago

            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.

            • Sylvartas@lemmy.dbzer0.com
              link
              fedilink
              English
              arrow-up
              2
              ·
              3 months ago

              Yeah I saw that. It’s weird because I’ve used it without a second thought in tons of different languages and never had issues with it

    • squaresinger@lemmy.world
      link
      fedilink
      arrow-up
      4
      arrow-down
      2
      ·
      edit-2
      3 months 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
        ·
        3 months 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
          3 months 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

      • OshaqHennessey@midwest.social
        link
        fedilink
        arrow-up
        4
        arrow-down
        1
        ·
        3 months ago

        Not in this case. First, i is declared and assigned a value of 0. Next, x is declared and assigned a value of -i or -0. On the first loop iteration, i will decrement to -1, perform the conditional check, then execute the loop body which will assign x to -i or -(-1) or positive 1, and so on.

        The only time a variable is created without a value is if you declare one without assigning a value like with

        [int]i;

        • BassTurd@lemmy.world
          link
          fedilink
          arrow-up
          5
          ·
          3 months ago

          I know. OP asked what x was before the loop, and I just said it’s an int. The int can be any value because as you pointed out it will be set to 0 in the first loop iteration.

          • OshaqHennessey@midwest.social
            link
            fedilink
            arrow-up
            2
            ·
            3 months ago

            Shit, you’re right. x is declared inside the loop, so it doesn’t exist until the loop begins execution.

            Technically, I suppose you could say the compiler will allocate memory for x without assigning a value before the loop is executed and… I’m understanding what you mean now, I think.

            • anton@lemmy.blahaj.zone
              link
              fedilink
              arrow-up
              1
              ·
              3 months ago

              The code seems to be C-style language with curly braces and types in front for variable declarations, probably java. This means the variable must be declared of screen before the loop or it would not compile. It could have a previous value or be uninitialized, but that does not affect the end result.

              • BassTurd@lemmy.world
                link
                fedilink
                arrow-up
                1
                ·
                3 months ago

                I read in on C but it’s also true for JavaScript. The code implies that x was declared as an int sometime previously, or if JavaScript, just an object if not assigned a value giving it a type.

              • OshaqHennessey@midwest.social
                link
                fedilink
                arrow-up
                1
                ·
                3 months ago

                Yeah, it does look like C now that I think about it. You’re right about the end result too. I believe C# will let you do inline declaration and assignment like that, so maybe that’s what we’re looking at? Been a while, could be wrong

    • OshaqHennessey@midwest.social
      link
      fedilink
      arrow-up
      1
      ·
      3 months ago

      If this is JavaScript, it would have a value of -0, which is actually valid and works the same as normal zero thanks to type coercion. I think the only difference is some methods that detect if a number is negative will return true instead of false, but otherwise, JS treats -0 the same way as 0

  • Atlas_@lemmy.world
    link
    fedilink
    arrow-up
    8
    ·
    3 months ago

    Oddly enough, out of all of these the one the compiler has the best chance of optimizing out is the last one

    • LeFantome@programming.dev
      link
      fedilink
      arrow-up
      2
      ·
      3 months ago

      What?

      First one is optimized obvious.

      Second one optimizes to x = 10 via constant propagation.

      Third one first unrolls the loop, propagates constants including booleans, and then eliminates dead code to arrive at x = 10.

      The last one cannot be optimized as “new” created objects that get used, nextInt() changes the state of those objects, and the global state of the random number system is impacted.

  • ulterno@programming.dev
    link
    fedilink
    English
    arrow-up
    24
    ·
    3 months ago

    That’s not even enough to get you a job these days.
    You now have to use:

    do {
        x = reinterpret_cast<int>(AI::Instance().ask("Do Something. Anything. Be efficient and productive. Use 10 tokens."));
    } while (x != 10);
    
    • Tetragrade@leminal.space
      link
      fedilink
      English
      arrow-up
      8
      ·
      3 months ago

      This isn’t just a function, it’s a bold restatement of what it means to write code — a symphony of characters, questioning the very nature of the cutting edge language models that I want to beat with hammers.

    • melfie@lemy.lol
      link
      fedilink
      arrow-up
      6
      ·
      3 months ago

      You’re absolutely right! Who sets a variable these days without running it though a LLM?

        • MonkeMischief@lemmy.today
          link
          fedilink
          arrow-up
          2
          ·
          edit-2
          3 months ago

          First, we’ll deep dive into “What is a variable?”, then together we’ll examine “Who sets a variable?”, “What is an LLM?” and finally, “Who would set a variable without using an LLM?”

          You’ll be a coding pro in no time!

          How does that sound?

          (I felt gross writing this lmao)

    • TheOakTree@lemmy.zip
      link
      fedilink
      arrow-up
      2
      ·
      3 months ago

      If only I could measure the quality of my paper purely by word count…

      I thought “a a a a a a” x100000 was thought-provoking and well tested.

  • Mika@piefed.ca
    link
    fedilink
    English
    arrow-up
    22
    arrow-down
    1
    ·
    3 months ago

    I once was helping to organize the testing of town-level algorithmic competition for school students.

    The competition had one entry level issue that was basically solvable by reading the question properly, recognising that it’s just multiplication of two numbers, and writing the simplest app ever.

    And there was one student who passed the automatic tests. We had to read the code too for the protocol, just to make sure there was no cheating.

    We looked in the code. What? Why? It had two nested for loops and a++ inside. When we understood what’s going on we couldn’t stop laughing for like solid ten minutes.

    • TheOakTree@lemmy.zip
      link
      fedilink
      arrow-up
      3
      ·
      3 months ago

      Multiplication is just repeated addition :) glad it worked for the kid, despite the… inefficiency.

    • cooligula@sh.itjust.works
      link
      fedilink
      English
      arrow-up
      3
      ·
      3 months ago

      I’d say Meta hiring someone to work on WhatsApp. Man, is that piece of software crap… Every update, a new UI bug/glitch appears

      • Hirom@beehaw.org
        link
        fedilink
        arrow-up
        3
        ·
        edit-2
        3 months ago

        An infinite loop canot be ruled out in the last case, so a compiler couldn’t optimize this away without potentially changing the program behavior.

            • yetAnotherUser@discuss.tchncs.de
              link
              fedilink
              arrow-up
              1
              ·
              3 months ago

              Even though this isn’t C, but if we take from the C11 draft §6.8.5 point 6 (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf):

              An iteration statement whose controlling expression is not a constant expression, that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation to terminate

              “new Random().nextInt()” might perform I/O though so it could still be defined behavior. Or the compiler does not assume this assumption.

              But an aggressive compiler could realize the loop would not terminate if x does not become 10 so x must be 10 because the loop can be assumed to terminate.

      • Yggstyle@lemmy.world
        link
        fedilink
        arrow-up
        4
        ·
        3 months ago

        Technically yes… But I think he was more making the excuse for the gore “from the goresmith’s perspective.”

        And I’m not sure if the compiler in any language would change a random check function… The others are a possibility.