Question regarding pow() function

Doug Gwyn gwyn at smoke.BRL.MIL
Thu Feb 2 08:37:17 AEST 1989


In article <400 at sunset.MATH.UCLA.EDU> tony at MATH.UCLA.EDU () writes:
>I'm trying to use the pow () function to raise negative one (-1)
>to the power n, i.e. flip the sign back and forth.

That's an extremely expensive way to do it.
Try
	sign = n % 2 ? -1 : 1;
or, for a loop, initialize sign to 1 or -1 then just keep doing
	sign = -sign;

>        printf ("%lf",pow (-1,n)) ;

This is utterly dependent on the automatic coercion done under ANSI C
when a prototype is in scope.  If you were to port this to a pre-ANSI
C environment, the arguments to pow() would not be coerced to doubles
and therefore would be imported by pow() as garbage values.  In fact
maybe your current compiler (with which I'm not familiar) doesn't
coerce the argument types, in which case it could explain your problem.

>(I know I can use if statements to see if the sign should be - or +
>but I'd rather use the pow() function. Its more elegant...)

Like hell it is.

>What is pow (0.,0.)?  I am getting a value 0 but shouldn't it be undefined?

Really 0^0 should be treated as 1 for most applications.



More information about the Comp.lang.c mailing list