In C, I was wondering whether there's an 'optimal' way to compute half-integer powers. In a nutshell, the problem is computing x^(n/2) (assuming n
is odd and decently small, and x
is a some float). Is there a major difference in performance/accuracy between sqrt(pow(x, n))
and pow(x, 0.5 * n)
? Or even reversed: pow(sqrt(x), n)
.
Is there some other implementation for handling this specific case of half-integers?
My first thought is that you would just use pow
and compute the whole thing in one call, but I feel like with floating point roundoff and things I'm losing some of the precision of the question that comes from the fact that this is explicitly a half-integer. I thought then maybe there's better error performance if you use pow
for raising to an integer power and let sqrt
handle the (1/2) part.
I also noticed that GSL has functions for computing small integer powers; would combining those functions with sqrt
be better than just using pow
?
I'm pretty new to scientific programming with C so I'm not sure where I would even go to look for implementations of something like this, and Google hasn't really turned anything up.