Keith Thompson said:
No, unary minus is not a multiplicative operator. It happens to be
mathematically equivalent to multiplication by -1 -- but then how do
you express "-1" without using unary minus? It's also mathematically
equivalent to subtraction from zero.
As "-1", of course. The C language didn't implement it in such a fashion.
But, it could been
implemented as context dependent token just like the use of zero, when used
in a pointer context,
is recognized as the null pointer. A negative integer constant, like "=1",
could've eliminated
both subtraction and unary minus.
Consider INT_MIN - INT_MIN, which is well defined and yields zero.
Defining it in terms of addition with a unary minus introduces a
possible overflow.
The keyword to your argument is _possible_. The compiler is free to use
associative, commutative, distributive, and other proofs of mathematics as
long as the correct result is produced.
For example, it could be done this way.
Given:
INT_MIN (-2147483647-1)
INT_MIN rewritten in terms of (-1):
INT_MIN ((-1)*2147483647+(-1)*1)
Difference of INT_MIN-INT_MIN, using integer promotions and distributive
multiplication,
and returning the correct result:
INT_MIN-INT_MIN
INT_MIN+((-1)*INT_MIN)
((-1)*2147483647+(-1)*1)+(-1)*((-1)*2147483647+(-1)*1)
(-2147483647+(-1))+(-1)*(-2147483647+(-1))
(-2147483647+(-1))+(-1)*(-2147483647+(-1))
(-2147483647+(-1))+2147483647+1
-2147483648+2147483647+1
-1+1
0
In fact, the specification _seems_ to encourage using equivalent
mathematical calculations if it eliminates known possible overflows and
underflows.
5.1.2.3 Program execution
"3 In the abstract machine, all expressions are evaluated as specified by
the semantics. An
actual implementation need not evaluate part of an expression if it can
deduce that its
value is not used and that no needed side effects are produced (including
any caused by
calling a function or accessing a volatile object)."
5.1.2.3 Program execution
"10 EXAMPLE 2 In executing the fragment
char c1, c2;
/* ... */
c1 = c1 + c2;
the ''integer promotions'' require that the abstract machine promote the
value of each variable to int size
and then add the two ints and truncate the sum. Provided the addition of two
chars can be done without
overflow, or with overflow wrapping silently to produce the correct result,
the actual execution need only
produce the same result, possibly omitting the promotions."
6.3.1.1 Boolean, characters, and integers
"2 ... If an int can represent all values of the original type, the value is
converted to an int;
otherwise, it is converted to an unsigned int. These are called the integer
promotions.48) All other types are unchanged by the integer promotions."
Rod Pemberton