is there any advantage to using this code
double x;
double square = pow(x,2);
instead of this?
double x;
double square = x*x;
I prefer x*x and looking at my implementation (Microsoft) I find no advantages in pow because x*x is simpler than pow for the particular square case.
Is there any particular case where pow is superior?
FWIW, with gcc-4.2 on MacOS X 10.6 and
-O3compiler flags,and
result in the same assembly code:
Assembles to:
So as long as you’re using a decent compiler, write whichever makes more sense to your application, but consider that
pow(x, 2)can never be more optimal than the plain multiplication.