pythonpython-3.xalgorithmrecursiontail-recursion# OverflowError: (34, 'Numerical result out of range') in writing a custom implementation of pow(x,n)

While implementing pow(x, n), for (x=2,and n=-2147483648), I am getting the below error:

Code :

```
class Solution:
def myPow(self, x, n):
flag = n < 0
n = -1 * n if flag else n
ans = None
if n % 2 == 0:
t = pow(x, n/2)
ans = t*t
else:
t = pow(x, (n-1)/2)
ans = x * t * t
return (1/ans) if flag else ans
if __name__ == "__main__":
Solution().myPow(2,-2147483648)
```

```
Traceback (most recent call last):
File "pw.py", line 16, in <module>
Solution().myPow(2,-2147483648)
File "pw.py", line 8, in myPow
t = pow(x, n/2)
OverflowError: (34, 'Numerical result out of range')
```

However, when i implement the same with n/2 and (n-1)/2 typecasted into int as below code snippet I get 0.0 as the output:

```
class Solution:
def myPow(self, x, n):
flag = n < 0
n = -1 * n if flag else n
ans = None
if n % 2 == 0:
t = pow(x, int(n/2))
ans = t*t
else:
t = pow(x, int((n-1)/2))
ans = x * t * t
return (1/ans) if flag else ans
if __name__ == "__main__":
Solution().myPow(2,-2147483648)
```

I am unable to figure out the cause.Is this due to stackoverflow since python interpreter doesn't handle tail recursion optimization, and the result returned is persisted since it is being used later for further calculations.

I am curious to know why the two cases differ.

Solution

`/`

in Python 3.x always performs **floating point** division, unlike in Python 2.x where the operands had to be explicitly casted. Therefore `type(n/2) == float`

whereas `type(int(n/2)) == int`

. Alternatively you could use `n // 2`

where `//`

performs floor/integer division.

The built-in `pow`

function returns a `float`

if either of the arguments are also `float`

s. Internally these are double-precision floating point numbers which can store up to ~10^{308} – much smaller than 2^{2147483648/2}, hence the `OverflowError`

.

- Python Jinja2 LaTeX Table
- Getting attributes of a class
- How can I print many significant figures in Python?
- How to allow list append() method to return the new list
- Calculate Last Friday of Month in Pandas
- Python type hint for Iterable[str] that isn't str
- How to iterate over a list in chunks
- How to exit the entire application from a Python thread?
- Running shell command and capturing the output
- How do I pass a variable by reference?
- Convert range(r) to list of strings of length 2 in python
- How can I get the start and end dates for each week?
- how to use send_message() in python-telegram-bot
- Python conditional replacement based on element type
- How can I count the number of items in an arbitrary iterable (such as a generator)?
- Find longest consecutive range of numbers in list
- Insert text in braces with asyncpg
- How does one put a link / url to the web-site's home page in Django?
- How to determine if a path is a subdirectory of another?
- Custom Keybindings for Ipython terminal
- FastAPI asynchronous background tasks blocks other requests?
- How to make sure that information from one file is duplicated into several text documents, without specific lines
- Installing a Python environment with Anaconda
- sklearn pipeline model predicting same results for all input
- Brew command not found after installing Anaconda Python
- How to get an XPath from selenium webelement or from lxml?
- Pipe PuTTY console to Python script
- How to align the axes of a figure in matplotlib?
- Persist ParentDocumentRetriever of langchain
- How to reset index in a pandas dataframe?