[comp.lang.c] why just a power operator?

gmt@arizona.edu (Gregg Townsend) (01/10/88)

Why is everyone so interested in a power operator compared to, say, 
square root or logarithm?  Just because Fortran has one???

gwyn@brl-smoke.ARPA (Doug Gwyn ) (01/11/88)

In article <3410@megaron.arizona.edu> gmt@arizona.edu (Gregg Townsend) writes:
>Why is everyone so interested in a power operator compared to, say, 
>square root or logarithm?

Basically, it's because exponentiation involving one or two integers
can usually be done much faster than through use of the floating-point
pow() function, and in the common case **2 can be highly optimized.
Also, as pointed out previously by someone else, pow() fails for
converted integers for which the operation makes perfect sense.

By the way, here is a generic integer-to-integer function written
in "old C" that I sometimes find myself using (since the language
doesn't take care of it for me); it uses the "binary exponent
representation" trick mentioned in eralier messages:

/*
	LPow -- long exponentiation (no overflow detection)

	last edit:	86/06/29	D A Gwyn

	SCCS ID:	@(#)lpow.c	1.3
*/

long
LPow( base, exponent )			/* returns base^exponent */
	register long	base;
	register long	exponent;
	{
	register long	result;		/* result accumulator */

	/* handle simple special cases separately: */

	if ( exponent == 0 )
		return 1;		/* includes 0^0 */
	else if ( base == 0 )
		return 0;		/* exp. < 0 should be EDOM */
	else if ( base == 1 )
		return 1;
	else if ( base == -1 )
#if 0	/* intended code: */
		return exponent % 2 == 0 ? 1 : -1;
#else	/* faster equivalent (suggested by Dan Levy of Teletype): */
		return (exponent & 1) == 0 ? 1 : -1;
#endif
	else if ( exponent < 0 )
		return 0;

	/* general case with exponent > 0: */

	result = 1;

	for ( ; ; )	/* LOOP INVARIANT: result*base^exponent */
		{
#if 0	/* intended code: */
		if ( exponent % 2 != 0 )
#else	/* faster equivalent (suggested by Dan Levy of Teletype): */
		if ( (exponent & 1) != 0 )
#endif
			result *= base;

#if 0	/* intended code: */
		if ( (exponent /= 2) == 0 )
#else	/* faster equivalent (suggested by Dan Levy of Teletype): */
		if ( (exponent >>= 1) == 0 )
#endif
			break;		/* result now stable */

		base *= base;
		}

	return result;
	}

jima@hplsla.HP.COM (jim adcock ) (01/12/88)

/*

Regards: overloading "->" for "pow"

Sorry, I should have known better than to try to
sell "software futures."

Overloading "->" MIGHT have been "nice" since
it has about the precedence and binding
required for a good power operator.

Unfortunately, as Bjarne points out, there are
three problems with this: 1) "->" expects a 
member on its right hand side -- you don't get
to "overload" C++ syntax. 2) to overload an
operator, at least one operand has to be a
class argument, see page 283 of the Mar86 printing.
3) It's likely that writing "x->y" would NOT be
interpreted by a human reader as "x to the y power"

Hopefully, if my release of the compiler HAD the
"->" overloading capability, I would have TRIED IT
before I show my mouth off !!!

Given C++ overloading restrictions, the following overload
of "^" shows what CAN be done in C++.  Unfortunately
"^" has poor precedence to be used as a "pow" operator,
so you'd have to parenthesize it:

mycubic = a[3]*(x^3) + a[2]*(x^2) + a[1]*x + a[0];

which MIGHT be better than writing:

mycubic = a[3]*pow(x,3) + a[2]*pow(x,2) + a[1]*x + a[0];

[to use a weak example!]

By analogy to using "<<" as the output operator, using "^"
to mean "pow" MIGHT be justifiable if you need it enough,
and use it enough in your code to make its use obvious
from context.

Again, IFF the power operator is needed by most users of a
language, then it should be in the language, otherwise
leave it out.

P.S. I HAVE tried the following, which is based on the half-baked
    idea I had in the back of my head when I shot my mouth off 
    last time!!!

*/

#include <stdio.h>
extern double pow(double x, double y);

class Double
{
public:
    double dval;
    Double(double val) {dval = val;};
};  

inline double operator^(Double& x, int y) 
{ return pow(x.dval, double(y)); }

main()
{ 
    int i;
    Double x = 2.0;
    
    for (i = 0; i <= 10; ++i) printf("%g \n",x^i);
}
    

decot@hpisod2.HP.COM (Dave Decot) (01/14/88)

My entry in the power operator contest is the following:

    a %% b

I do NOT want to hear anything about "logical modulus" operators.

Dave Decot
hpda!decot