[Python] 무한대 표시(양의 무한대 inf, 음의 무한대 -inf)

우니·2022년 12월 1일
0

알고리즘 문제들을 풀면서 최소값 및 최대값을 구할때 충분히 큰 수를 넣거나 양, 음의 무한대를 사용해야하는 순간들이 있다.

>>> positive = float("inf")          # 양의 무한대
>>> print(positive)
inf

>>> negative = float("-inf")         # 음의 무한대
>>> print(negative)
-inf

>>> positive = int(float("inf"))
>>> print(positive)
OverflowError: cannot convert float infinity to integer         # 에러 발생

>>> negative = int(float("-inf"))
>>> print(negative)
OverflowError: cannot convert float infinity to integer         # 에러 발생

무한수는 float형에만 적용가능해서, int 형은 불가능.

0개의 댓글