Thread: Excel Math Bug
View Single Post
  #107   Report Post  
Posted to microsoft.public.excel.programming,sci.math
Harlan Grove Harlan Grove is offline
external usenet poster
 
Posts: 733
Default Excel Math Bug

Phil Carmody wrote...
....
He probably means that unary minus has a significantly higher

precedence
than subtraction, so much so that it has a strictly higher precedence
than at least one operator that one would commonly view as being of a
strictly higher precdence than subtraction.


No kidding! Gee, I'd never have guessed. How odd that this makes C
just like FORTRAN. I've been involved in a few back & forth
discussions with Alan, and I have little doubt he misconstrued C's ^
operator.

FWLIW, the precedence of unary minus/negation and multiplication,
division and remainder operators just doesn't matter. Considering C's
*,

(a * b) == (-a) * (-b) == -(a * (-b))
(-a * b) == (-a) * b == -(a * b)
(a * -b) == a * (-b) [b's - can't be parsed otherwise]
(-a * -b) == (-a) * (-b) == (-a * (-b)) == -(a * (-b))

Unary minus *could* have been given equal to or lower precedence than
*, / and %, and it wouldn't have mattered. I suspect the reason it's
higher is that it was supposed to be given the same as -- and ++, and
those are given higher precedence than *, / and %. Much, much easier
giving all unary operators the same precedence, so they'd be resolved
by the associativity rule alone.

Therefore there is a concrete example of an expression which is

interpretted
differently in C than it would be using people like you's

conventions.

First, what are my conventions? Second, if my favored conventions
exactly matched those of Excel, how would that matter vis-a-vis
negation/unary minus and *, / or %? While the machine may mechanically
parse unary minus earlier than *, / or %, the results would be the
same if it parsed unary minus after.

In C's case, the precedence inversion is with the multiplicative

family
of operators. e.g. compare C's interpretation of -2%3 as (-2)%3

rather than
-(2%3).


OK, it matters for remainder, but because there's residual ambiguity
about what (-2) % 3 should return. Is the answer -2 or +1? This is
also a matter of convention, and there are differences of opinion.
FWIW, C and Excel differ here, but it's due to the sign convention of
the remainder operator, not to operator precedence. In C, -2 % 3 ==
(-2) % 3 == -(2 % 3) == -2, but in Excel MOD(-2,3) == 1 != or <
-MOD(2,3) == -2. Note that because Excel uses a function for
remainder, unary minus's operator precedence is immaterial; the signs
of both arguments to the remainder function are necessarily obvious.