in reply to Re^3: perl arithmetic is killing me! HELP!
in thread perl arithmetic is killing me! HELP!

I think there's at least one more condition, namely: Do not assign a value that's greater than UINT_MAX.

No, that's not a precondition, because not assigning a value greater than (or equal to) "UINT_MAX" is exactly what the OP is trying to do.

but the result is one that I do not expect

It is not only the result the OP wants, it's the result you should expect since Perl's integer addition is implemented as a C integer addition. The result should be the 32 lower-order bits of the sum.

Alas, such handling of this corner case

integer clearly says it "only affects how most of the arithmetic and relational operators handle their operands and results, and not how all numbers everywhere are treated" and proceeds to list the specifics.

  • Comment on Re^4: perl arithmetic is killing me! HELP!

Replies are listed 'Best First'.
Re^5: perl arithmetic is killing me! HELP!
by syphilis (Archbishop) on Nov 08, 2018 at 09:13 UTC
    No, that's not a precondition, because not assigning a value greater than (or equal to) "UINT_MAX" is exactly what the OP is trying to do.

    Well ... it may be that the OP is already attempting to adhere to that precondition - I haven't studied the details.
    But it's still a precondition, and one of which we would best be fully aware.

    It is not only the result the OP wants, it's the result you should expect since Perl's integer addition is implemented as a C integer addition.

    If you read carefully what I wrote you'll see that I have no issue with the implementation of the integer addition. I do, however, have disdain for the way that overflowed integer values are assigned under the integer pragma. (The reason that I don't like that behaviour is because it differs from C.)

    integer clearly says it "only affects how most of the arithmetic and relational operators handle their operands and results, and not how all numbers everywhere are treated" and proceeds to list the specifics

    Firstly, that's good to hear - and no less than what I would expect of perl documentation.
    Secondly, let's recommend that the OP carefully reads that documentation. The first question asked was "Can somebody explain to me how to do 32-bit operations in Perl?".
    So, if 'use integer' is the preferred option, then I think harangzsolt33 may well benefit from being fully aware of all caveats relating to this pragma.

    Cheers,
    Rob

      But it's still a precondition, and one of which we would best be fully aware.

      Again, something can't be a precondition for itself. You can't say that doing X is a precondition for doing X. It makes no sense.

        You can't say that doing X is a precondition for doing X

        And there are none so blind as those that cannot see.

        Cheers,
        Rob