1.

Python library has built-in pow() function. It also has pow() function in math module. What's the difference?

Answer»

Built-in pow() function has two variations  - with two arguments and three arguments.

The pow() function with two arguments returns first argument raised to second argument. That means pow(x,y) results in x to the power y

>>> pow(10,2) 100

The two-argument form pow(x, y) is equivalent to using the power operator: x**y.

The pow() function can take three arguments. In that case, pow(x,y,z) returns pow(x,y)%z. It returns modulo division of x to the power y and z.

>>> pow(10,2) 100 >>> pow(10,2,3) 1

This is equivalent TO10**2%3 which is 100%3 = 1

The pow() function from math module only has a two argument version. Both the arguments are treated as float. Hence the result is always float even if arguments are INT.

>>> math.pow(10,2) 100.0 >>> pow(10,2) 100

Because of the FLOATING point representation in the memory, result of math.pow() may be inaccurate for integer arguments. Hence for INTEGERS it is advised to use built-in pow() function or ** operator.



Discussion

No Comment Found