파이썬(Python) 에러 OverflowError 와 처리 방법
2월 18, 2023
In Python |
파이썬(Python) 에러 OverflowError
원인
Python의 OverflowError
는 계산 결과가 너무 커서 처리할 수 없을 때 발생합니다.
아래는 OverflowError가 발생할 수 있는 코드의 예입니다.
File: test.py
a = 10**12345
b = 6
print(a/b)
결과
$ python test.py
File "/test.py", line 3, in <module>
print(a/b)
OverflowError: integer division result too large for a float
a / b
의 결과가 float의 범위를 벗어나는 값이기 때문에 오류가 발생합니다.
다른 경우도 있습니다.
File: test2.py
import sys
sys.setrecursionlimit(10**12)
결과
$ python test2.py
File "/test2.py", line 2, in <module>
sys.setrecursionlimit(10**12)
OverflowError: Python int too large to convert to C int
sys.setrecursionlimit
은 C의 int
로 변환할 수 있는 값만 넣을 수 있게 되어 있기 때문에 에러가 발생합니다.
> sys.setrecursionlimit는 파이썬의 재귀 깊이 제한을 설정하는 함수입니다.
처리 방법
사용하는 값을 더 낮은 값으로 설정해야 합니다.