3 ms·
In the C language, -a*b parses as (-a)*b, but c-a*b parses as c-(a*b). Unary minus has a higher precedence than binary operators. You don't notice because t
by kazinator 2y ago
In the C language, -a*b parses as (-a)*b, but c-a*b parses as c-(a*b).
Unary minus has a higher precedence than binary operators.
You don't notice because the semantics allows the sign to move around, unlike with exponentiation.
But when we throw in edge cases involved in undefined behavior, oops!
0 - INT_MIN/2 // fine: parses as 0 - (INT_MIN / 2)
-INT_MIN/2 // not okay: parses as (-INT_MIN) / 2
The INT_MIN value need not have an additive inverse because of a quirk in two's complement.
- MaxBarraclough 2y ago> The INT_MIN value need not have an additive inverse because of a quirk in two's complement. For comparison, in Java, these 3 expressions each yield Integer.MIN_VALUE (i.e. -2147483648): -Integer.MIN_VALUE Integer.MIN_VALUE * -1 Integer.MIN_VALUE / -1 I have to admit I expected all 3 to throw. edit On reflection I shouldn't have expected that, I recall reading John Regehr's blog post on the downsides of how Java defaults to wrapping behaviour: https://blog.regehr.org/archives/1401 https://blog.regehr.org/archives/1401