in reply to perl arithmetic is killing me! HELP!

& only works on numbers that fit in an IV (a signed integer type) or UV (an unsigned integer type). This range is -2,147,483,648..4,294,967,295 for you. Without use integer;, 872_415_232 + 4_278_190_080 produces a number larger than that, so & produces junk. Specifically, it returns the largest UV (4,294,967,295 for you), which %d casts to an IV (-1).

Replies are listed 'Best First'.
Re^2: perl arithmetic is killing me! HELP!
by harangzsolt33 (Deacon) on Nov 07, 2018 at 15:50 UTC
    Got it! Thanks. But you know, in many other programming languages, it doesn't work like that. For example, both JavaScript and Perl use 64-bit IEEE-754 double precision floats for numbers. So, doing the same complex calculation in Perl and JavaScript, one should get the SAME RESULT. That was my assumption. But that's not the reality.

      Perl is closer to C than to JavaScript.

      $ cat a.c int main() { double x = 1; double y = 1; int z = x & y; return 0; } $ gcc -Wall -Wextra -pedantic a.c -o a && a a.c: In function ‘main’: a.c:4:15: error: invalid operands to binary & (have ‘double’ and ‘doub +le’) int z = x & y; ^ a.c:4:9: warning: unused variable ‘z’ [-Wunused-variable] int z = x & y; ^

      And the underlying reason for that is that CPUs simply can't perform bitwise-AND on floats (because it makes little sense to do so).