ma****@gmx.at wrote:
so you mean in that way:
[code]
double power(double x, int n)
{
if (n==0)
return 1.0;
if (0 == n%2)
{
return (x^((n-1)/2))*(x^((n-1)/2))
The ^ operator performs a bitwise exclusive or.
In the standard C library, there is a function called pow for
exponentiation.
#include <cmath>
double p = pow(x, n);
or did i interpret something wrong...?
I think Victor was just pulling your leg. It would be pretty dumb to
call pow() twice on the halved exponent, and then multiply the two
results together.
// silly!
double p = pow(x, n/2) * pow(x, n/2);
Firstly, there are the extra divisions and multiplication. These not
only add overhead but possible loss of precision due to truncation
errors. Secondly, the compiler might not optimize the redundant call to
pow away, and so the program may actually end up exponentiating twice.