Quote:
std::cout << -2*2;
document.write(-2^2);
WScript.Echo(-2^2);


The first one is kinda cheating, -2*2 wasn't being discussed smile

And for all 3 (C,C++,Jscript, Javascript, Java) -2^2 is something entirely different, so it's not useful either. Apparently there's no power operator in those languages, and a Math.pow(x) function makes you choose anyway...

For the interested, ^ is the bitwise XOR function in those languages, has lower precedence than lots of stuff including * (multiplication) / (division) + (binary addition) - (binary subtraction). Incidentally, it gives the correct (or wrong?) result due to the nature of the 2-complement integer representation in computers.

Perl is kinda strange imo, it says unary - has higher precedence than **, but it still gives -4 as answer, while *, % and other operators (below unary - and below **) do function as expected by the precedence.

Let's keep it at: use brackets crazy

As a reference:
C operator precedence
Java operator precedence
(google has the others)
Two's complement


As for my first reply, apparently I was wrong, I did learn some new stuff though smile