Path: utzoo!utgpu!water!watmath!clyde!rutgers!sdcsvax!sdcc6!ix426 From: ix426@sdcc6.ucsd.EDU (tom stockfish) Newsgroups: comp.lang.c Subject: Re: Power operator? Message-ID: <3565@sdcc6.ucsd.EDU> Date: 7 Jan 88 01:47:22 GMT References: <47000029@uxe.cso.uiuc.edu> Reply-To: ix426@sdcc6.ucsd.edu.UUCP (tom stockfish) Organization: University of California, San Diego Lines: 53 In article <47000029@uxe.cso.uiuc.edu> mcdonald@uxe.cso.uiuc.edu writes: >I've been wondering why there is no power operator in C. Did the ANSI >committee consider adding one, and if so, why wasn't it done? I'm >referring to the construct in fortran X**2 or X**7 . This would seem >to be extremely useful and trivial to implement. > >Doug McDonald Useful, yes, but fortran syntax (x**y) already means "fetch the value at address y and multiply by x". For example, double x = 2; double y = 3; double *z = &y; printf( "%f\n", x**z ); prints "6". My only complaint about C's operator symbols is that "*" is grossly overworked. For example, the following code might look o.k., but do you see what's wrong? In fact, it even compiled (!) with no error messages or warnings on the Berkeley Unix's cc a few years back (no, it didn't run properly). f( x, y ) double *x, *y; { return *x/*y; } Of course, Everyone Knows that polynomials should never be calculated as, e.g. a*x**4 + b*x**3 + c*x**2 + d*x + e (using fortran notation), so that an exponential operator shouldn't be used here, but there are other important uses such as (again using fortran) f(x) ** 2 where I don't want to call f() twice, and I don't want to clutter up my code with temporaries. The syntax x^y is bad because this is already defined for integers as bit-wise exclusive or. x^^y is a possibility, altho those who want ^^ for logical exclusive or will I'm sure complain. x%%y would probably be o.k., but it looks pretty wierd. I suppose x!y would work, but I would guess that the numerical people who want exponentiation as an operator would confuse ! with factorial. How about x@y ? How about x..y ? How about some better suggestions? -- || Tom Stockfisch, UCSD Chemistry tps@chem.ucsd.edu