반응형
Python 2.7에서 inf
및 -inf
를 int
로 정의하는 방법이 궁금합니다. 시도했지만 inf
및 -inf
는 float
로만 작동합니다.
a = float('-inf') # works
b = float('inf') # works
c = int('-inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf'
d = int('inf') # compile error, ValueError: invalid literal for int() with base 10: 'inf'
해결 방법
댓글에서 말한 내용을 요약하면
파이썬에서 무한대를 정수로 표현하는 방법은 없습니다. 이것은 다른 많은 언어의 동작과 일치합니다. 그러나 Python의 동적 타이핑 시스템으로 인해 정수 대신 float ( 'inf')
를 사용할 수 있으며 예상대로 작동합니다.
참조 페이지 https://stackoverflow.com/questions/40445920
반응형
'파이썬' 카테고리의 다른 글
파이썬 How do you uninstall a python package that was installed using distutils? (0) | 2020.10.27 |
---|---|
파이썬 Add a new sheet to a existing workbook in python (0) | 2020.10.27 |
파이썬 Python : OverflowError : 수학 범위 오류 (0) | 2020.10.26 |
파이썬 Python source code collection (0) | 2020.10.26 |
파이썬 Python 3에서 URL 인코딩하는 방법은 무엇입니까? (0) | 2020.10.26 |
댓글