Today, I was doing the leetcode 50 MyPow , and I decided to use the recursion to achieve it. My code is as follows:
class Solution:
def myPow(self, x: float, n: int) -> float:
if n > 0:
return self.recur(x, n)
else:
return 1.0 / self.recur(x, -n)
def recur(self, x: float, n: int) -> float:
if n == 0:
return 1.0
else:
y = self.recur(x, n // 2) ** 2
return y if n % 2 == 0 else y * x
But this code doesn't work and output the error:
OverflowError: (34, 'Numerical result out of range')
[Previous line repeated 19 more times]
y = self.recur(x, n // 2) ** 2
Line 13 in recur (Solution.py)
y = self.recur(x, n // 2) ** 2
Line 13 in recur (Solution.py)
y = self.recur(x, n // 2) ** 2
Line 13 in recur (Solution.py)
return 1.0 / self.recur(x, -n)
Line 7 in myPow (Solution.py)
ret = Solution().myPow(param_1, param_2)
Line 40 in _driver (Solution.py)
_driver()
Line 51 in <module> (Solution.py)
Then I try to modify these lines :
y = self.recur(x, n // 2) ** 2
return y if n % 2 == 0 else y * x
to
y = self.recur(x, n // 2)
return y*y if n % 2 == 0 else y *y* x
Finally, it worked. But why? And what about the difference between them?
Add the failed case:
x = 2.00000
n = -2147483648
Is someone who can answer my confusion?
This is because the multiplication returns
infwhen the result doesn't fit in thefloattype, while the power**throws an exception. For example, this printsinf:while this throws an OverflowError:
There's probably some explanation of this in PEPs, but I'm not aware of it.
As pointed out in the comments, you need an arbitrary precision type here, not a float. Consider:
This would print the result correctly.