Exponentials in python x.**y vs math.pow(x, y)

后端 未结 5 2029
臣服心动
臣服心动 2020-11-27 03:32

Which one is more efficient using math.pow or the ** operator? When should I use one over the other?

So far I know that x**y can return an int

5条回答
  •  [愿得一人]
    2020-11-27 04:11

    The pow() function will allow you to add a third argument as a modulus.

    For example: I was recently faced with a memory error when doing

    2**23375247598357347582 % 23375247598357347583

    Instead I did:

    pow(2, 23375247598357347582, 23375247598357347583)

    This returns in mere milliseconds instead of the massive amount of time and memory that the plain exponent takes. So, when dealing with large numbers and parallel modulus, pow() is more efficient, however when dealing with smaller numbers without modulus, ** is more efficient.

提交回复
热议问题