[python] Exponentiation in Python - should I prefer ** operator instead of math.pow and math.sqrt?

In my field it's very common to square some numbers, operate them together, and take the square root of the result. This is done in pythagorean theorem, and the RMS calculation, for example.

In numpy, I have done the following:

result = numpy.sqrt(numpy.sum(numpy.pow(some_vector, 2)))

And in pure python something like this would be expected:

result = math.sqrt(math.pow(A, 2) + math.pow(B,2)) # example with two dimensions.

However, I have been using this pure python form, since I find it much more compact, import-independent, and seemingly equivalent:

result = (A**2 + B**2)**0.5   # two dimensions
result = (A**2 + B**2 + C**2 + D**2)**0.5

I have heard some people argue that the ** operator is sort of a hack, and that squaring a number by exponentiating it by 0.5 is not so readable. But what I'd like to ask is if:

"Is there any COMPUTATIONAL reason to prefer the former two alternatives over the third one(s)?"

Thanks for reading!

This question is related to python math exponentiation

The answer is


Even in base Python you can do the computation in generic form

result = sum(x**2 for x in some_vector) ** 0.5

x ** 2 is surely not an hack and the computation performed is the same (I checked with cpython source code). I actually find it more readable (and readability counts).

Using instead x ** 0.5 to take the square root doesn't do the exact same computations as math.sqrt as the former (probably) is computed using logarithms and the latter (probably) using the specific numeric instruction of the math processor.

I often use x ** 0.5 simply because I don't want to add math just for that. I'd expect however a specific instruction for the square root to work better (more accurately) than a multi-step operation with logarithms.