Would it be more stable (or faster or even different) to even implement a simple function to perform a iteration repetition of log (n), if it is known that the exponent is an integer?
The squaring result for integer exponents is generally less accurate than pow , but both are stable in the sense that cramped inputs produce close results. You can expect exponentiation by squaring to introduce 0.5 ULP relative error by multiplication (for example, 1 ULP error to calculate x 3 as x * x * x ).
When the second argument n is statically known as 2, then, in any case, we implement x n as x * x . In this case, it is faster and more accurate than any possible alternative.
How std :: sqrt (x) compares, in stability, with something like the form std :: pow (x, k / 2)
Firstly, sqrt accuracy cannot be exceeded for implementing IEEE 754 because sqrt is one of the basic operations that this standard requires as accurate as possible.
But you are not asking about sqrt , you are asking (I think) about <calculation x n > * sqrt (x) as opposed to pow(x, n + 0.5) . Again, in general, for a quality implementation of pow you can expect pow(x, n + 0.5) be more accurate than alternatives. Although sqrt(x) will be computed to 0.5 ULP, multiplication introduces its own approximation to 0.5 ULP, and in general, it is better to get the result that interests you in one call to a well-implemented function. A high-quality implementation of pow will give you 1 ULP accuracy for your result, and the best implementations “guarantee” 0.5 ULP.
And as a bonus, what could be the difference in speed?
If you know in advance that the indicator will be a small integer or a multiple of 0.5, then you have information that the pow artist does not have, so you can defeat them, at least at the expense of cost, to determine that the second argument is small integer. In addition, the developer of a quality implementation seeks a more accurate result than simple exponentiation by squaring. On the other hand, the pow developer can use extremely sophisticated methods to minimize average execution time, despite better accuracy: see, for example, the implementation of CRlibm . I put the verb “guarantee” higher inside the quotation marks when I spoke about the best implementations of pow , because pow is one function for which the CRLibm 0.5 ULP accuracy guarantee is only “with astronomical probability” .