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

The actual requirements:
•That you 'use integer'
•That the size of your perl's integer is 4 bytes (check perl -V:ivsize) or that you use & 0xFFFF_FFFF


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

I don't know what others expect to happen when, having chosen to 'use integer', one assigns (to a perl scalar) a value that is greater than UINT_MAX, but the result is one that I do not expect.
This is behaviour that I do expect when ivsize is 4:
C:\> perl -Minteger -wle "$x = 4294967295 + 3;print $x;" 2
And I expect the same output when I do:
C:\> perl -Minteger -wle "$x = 4294967298;print $x;" 4294967298
Alas, such handling of this corner case leaves me feeling uneasy about using the integer pragma at all.

How many other surprises (or traps for the unwary) are to be found in the finer details of this pragma's behaviour ?

Cheers,
Rob

Replies are listed 'Best First'.
Re^4: perl arithmetic is killing me! HELP!
by ikegami (Patriarch) on Nov 08, 2018 at 08:06 UTC

    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.

      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.