65

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!

8
  • 1
    This might be a relevant thread - stackoverflow.com/questions/327002/… Commented Sep 23, 2013 at 17:43
  • 1
    I tend to think that math.sqrt() is more readable than ()**0.5. Commented Sep 23, 2013 at 17:48
  • 1
    @Maxime, I'd go one further and just use sqrt with from math import sqrt. Commented Sep 23, 2013 at 17:54
  • 1
    For my taste, the ABCD lines are ugly. Why not use a tuple or list as a vector? result = sum([x**2 for x in v])**0.5
    – Robᵩ
    Commented Sep 23, 2013 at 17:54
  • 1
    The computational issues are discussed in the link in the first comment. In short, pow and ** are identical. sqrt calls the C library. sqrt goes faster, but can't do Complex.
    – Robᵩ
    Commented Sep 23, 2013 at 18:04

2 Answers 2

64

math.sqrt is the C implementation of square root and is therefore different from using the ** operator which implements Python's built-in pow function. Thus, using math.sqrt actually gives a different answer than using the ** operator and there is indeed a computational reason to prefer numpy or math module implementation over the built-in. Specifically the sqrt functions are probably implemented in the most efficient way possible whereas ** operates over a large number of bases and exponents and is probably unoptimized for the specific case of square root. On the other hand, the built-in pow function handles a few extra cases like "complex numbers, unbounded integer powers, and modular exponentiation".

See this Stack Overflow question for more information on the difference between ** and math.sqrt.

In terms of which is more "Pythonic", I think we need to discuss the very definition of that word. From the official Python glossary, it states that a piece of code or idea is Pythonic if it "closely follows the most common idioms of the Python language, rather than implementing code using concepts common to other languages." In every single other language I can think of, there is some math module with basic square root functions. However there are languages that lack a power operator like ** e.g. C++. So ** is probably more Pythonic, but whether or not it's objectively better depends on the use case.

3
  • Where does operator.pow fall into the mix? Commented May 12, 2015 at 4:53
  • 1
    Excellent answer, particularly for taking what could of been a highly 'opinionated' thread and focusing in on the OP, as well as essential Python tenants. Kudos.
    – GrayedFox
    Commented Oct 6, 2016 at 16:06
  • However, if the inputs are integers, ** or the built-in pow function may be preferable computationally: "Unlike the built-in ** operator, math.pow() converts both its arguments to type float. Use ** or the built-in pow() function for computing exact integer powers." - Python docs: math library docs.python.org/2/library/… Commented Jan 23, 2018 at 14:50
18

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.

2

Not the answer you're looking for? Browse other questions tagged or ask your own question.