jacob navia wrote On 12/23/05 14:34,:
Eric Sosman a écrit :
jacob navia wrote:
[...]
The purported exponentiation operator would have to be dyadic:
10 ^^ 3 --> 1000
This would be feasible but the problem is inserting it in
the precedence rules of C
10+4 ^^ 4+5 --> pow(14,9) ? or 10+pow(4,4)+5 ???
There are enomous problems with this approach, and for exponentiation
I think it is just not worth the effort, unless we devise a general
rule about adding new operators.
The "enomous [sic] problems" you mention had already been
solved when I began programming thirty-nine years ago.
2**3**4
is an error in fortran. Should that be also the case in C?
It's been so long since I used FORTRAN that I don't
remember. If it were up to me, I'd say exponentiation
should be right-associative and let it go at that. It's
not up to me, though.
2|3|4 is legal C... Should the operator ** be the same?
Besides, ** is not really good since it is easily mistaken with
multiply by the dereferenced pointre:
a**b is
(a)*(*b)
or
(a)**(b) ???
Etc etc. Once you start you will see it is NOT that easy.
This is just a question of spelling. Obviously you
can't grab ** or ^ or % to be C's exponentiation operator;
they're already taken for other purposes. # would almost
work, but there'd be niggling problems with line boundaries.
$ would work, but there's a distaste for national characters.
Someone has already suggested ^^, and I think @ would also
be all right. The only question here is the choice of a
character combination that isn't already in use and has
some amount of mnemonic value. We could even do without
the mnemonic: nobody seems to complain about % even though
it's not very suggestive of "remainder."
1)
We have operators that have hardware support but not operator
support in C:
char a = 200;
char b = 200;
char c = a |+| b; // c is 255 not 400%255. No 'wrap around'
The operator I miss even more than exponentiation is
"integer multiply producing double-width product." I've
only run into one non-assembly language that provided such
a thing; that language used * to get a 16-bit product from
two 16-bit integers and '*' (with the quotes) to get a 32-
bit product. Among other things, it gave a very easy way
to "pick a card, any card" with reasonable accuracy:
card = (rand() '*' 52) >> 16
(Please take this with a grain of salt; I remember the '*'
distinctly because it was both unusual and pleasant to have,
but I can't vouch for the spelling of the right-shift
operator or for the name of the pseudo-random source. Read
the idiom, not my idiocy.)
But this is the reverse of the O.P.'s question: he didn't
ask why there aren't operators for all the useful machine
instructions, but why there's no exponentiation operator.
When he says the FAQ offers an excuse rather than an answer
I'm inclined to agree with him. After all, C has no qualms
about requiring things like `long long' and `long double'
support even on 8-bit embedded toaster controllers; why be
squeamish about requiring exponentiation? The FAQ's answer
just doesn't seem to hold a lot of water.
I think the "why not?" question can only be answered
by dmr himself. Anybody else is just speculating.