Calculate exponent - Difference between pow () and **

2

As I said in the title, I found two ways to calculate the exponent in python and both work for me,

a = 2
print (a**2)
print (pow(a,2))

Both give the same, what is the difference? When to use one or the other?

Health and thanks!

    
asked by NEA 08.10.2018 в 19:16
source

3 answers

4

Both ** and pow() can be used to calculate integer or fractional powers. For example, 2**0.5 will give you the square root of two, and pow(2,0.5) the same.

The fundamental difference is that pow() always converts its arguments to float and returns a float result (no, see update). Instead, ** , when it detects that its arguments are integers, performs whole arithmetic. This can be much faster for small powers and, on the other hand, can produce results of arbitrary precision for large exponents (with all the significant figures that are necessary).

For example, compare the results of pow(2,1000) and 2**1000 .

>>> math.pow(2, 1000)
1.0715086071862673e+301
>>> 2**1000
1071508607186267320948425049060001810561404811705533607443750
3883703510511249361224931983788156958581275946729175531468251
8714528569231404359845775746985748039345677748242309854210746
0506237114187795418215304647498358194126739876755916554394607
7062914571196477686542167660429831652624386837205668069376

Update

It seems that pow() can also use integer arithmetic when the operands are integers. This was not the case in previous versions of python, it should be a more recent optimization, but it can be seen that type(math.pow(2,2)) produces int , so it is probably equivalent to ** , except when dealing with large numbers, which pow() represents floating point but ** goes to use "big ints".

Update 2

Another difference is that the operator ** can be "overloaded". That is, you can define a class that implements a __pow__() method that is invoked by the interpreter when it finds ej ** n , if ej is an object of that class (and then passes n as a parameter). Instead math.pow() necessarily requires that their arguments are numeric.

Example of the latter:

class P:
  def __init__(self, a, b):
    self.a = a
    self.b = b

  def __pow__(self, n):
    return (self.a + self.b)**n


ej = P(1, 2)
print(ej**2)

And 9 comes out. While:

print(math.pow(ej, 2))

produces the error:

Traceback (most recent call last):
  File "python", line 14, in <module>
TypeError: must be real number, not P
    
answered by 08.10.2018 / 20:23
source
1

As you put it in the example, there is no difference in using one form or another. if we consult the documentation :

  

Return x to the power and; if z is present, return x to the power and,   z module (computed more than pow (x, y)% z). The   two-argument form pow (x, y) is equivalent to using the power operator:   x ** and .

That is, in the form of x raised to y , both are equivalent . For how they are implemented, ** surely involves more code than pow() , we must remember that ** represents an internal implementation of some objects, the method __pow__ (possibly __rpow__ , __ipow__ ), but the impact, surely it is indistinguishable.

The only differences that could be significant in other circumstances:

  • pow() offers a third parameter z to obtain the module, using the binary notation, that is ** would be (x ** y) % z , in case of using the function, it would be pow(x, y, z) , this way , could offer a marginal improvement in performance.

  • The use of the binary operator, could eventually generate some confusion, eg: -2**2 actually, for a subject of precedence of the operators, is not the square of -2 but -(2**2) , to write it correctly: (-2)**2 . Using the function you would not need to remember this: pow(-2, 2)

Important : We are comparing the included function pow() with the binary operator ** , there is also another implementation in math.pow that does not enter this response.

    
answered by 08.10.2018 в 20:45
0

It is used ** to calculate powers of exact integers, on the other hand pow () converts the arguments to floating type.

You can find more information here: link

    
answered by 08.10.2018 в 19:20