What is the best way to display in Python? Double asterisk or Math.Pow?

Asked

Viewed 215 times

4

How to best expose in Python?

I must use the operator ** or math.pow?

Example math.pow:

> math.pow(3, 4);
#Imprime: 81.0

Example with double asterisk?

> 3 ** 4
#Imprime : 81

What I must take into account when using them?

1 answer

5


Speed

The operator ** is usually faster than math.pow(). He calls the native function pow, which, by the way, accepts a module argument. Usually it is better for integers.

Type safety

math.pow() always returns a float. In this case, when the guys are float, math.pow() is faster.

I removed it from here.

  • But if we want to enter in this "security of types" with the number 3 for example, wanting him to return float, we could just do 3.0 ** 3, right?

  • Yes, because if there’s even one float involved in the operation, the result is a float also.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.