In my area, the square number of numbers is very common, manage them together and take the square root of the result. This is done in the Pythagorean theorem and, for example, the calculation of RMS.
In numpy, I did the following:
result = numpy.sqrt(numpy.sum(numpy.pow(some_vector, 2)))
And in pure python, something like this was expected:
result = math.sqrt(math.pow(A, 2) + math.pow(B,2)) # example with two dimensions.
However, I used this pure python form, as I find it more compact, import-independent, and apparently equivalent:
result = (A**2 + B**2)**0.5
I heard that some people claim that the ** operator is a kind of hack, and squaring a number by exposing it to 0.5 not readable. But I would like to ask if:
"Is there any COMPUTER reason to prefer the previous two alternatives over the third (s)?"
Thanks for reading!
python math exponentiation
heltonbiker Sep 23 '13 at 17:40 2013-09-23 17:40
source share