Python의 올인원 무한대 값입니다. 어떤 물체와도 비교할 수 있습니다.
float('inf')
및 float('-inf')
가 있습니다. 그러나 이는 단순히 부동 소수점 무한대 값을 나타냅니다. 나는 비교 가능한 어떤 객체와도 비교할 수 있는 클래스를 만들고 싶었습니다.float('inf')
작성은 inf
에 비해 서투릅니다.pow(1, float('inf'))
1을 반환하지만 정의되지 않아야 합니다. 무한대에서 이 작업은 TypeError
반환합니다.간단히 pypi에서 패키지를 가져오세요.
pip 설치 무한대
지원되는 Python 버전:
Infinity
클래스는 다양한 비교 방법을 지원합니다.
>> > import sys
>> > from datetime import datetime
>> > from infinity import inf
>> > 3 < inf
True
>> > datetime ( 2000 , 2 , 2 ) < inf
True
>> > - inf < inf
True
>> > inf == inf
True
>> > - inf == - inf
True
또한 산술 연산자도 지원합니다.
>> > inf + inf
inf
>> > - inf - inf
- inf
>> > inf + 3
inf
>> > inf + datetime ( 2000 , 2 , 2 )
inf
>> > 5 / inf
0
>> > 3 / - inf
0
>> > pow ( inf , 0.5 )
inf
다음 작업은 TypeError
예외를 발생시킵니다.
>> > inf - inf
Traceback ( most recent call last ):
...
TypeError : unsupported operand type ( s ) for - : 'Infinity' and 'Infinity'
>> > - inf + inf
Traceback ( most recent call last ):
...
TypeError : unsupported operand type ( s ) for + : 'Infinity' and 'Infinity'
>> > inf / inf # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback ( most recent call last ):
...
TypeError : unsupported operand type ( s ) for / : 'Infinity' and 'Infinity'
>> > inf * 0
Traceback ( most recent call last ):
...
TypeError : unsupported operand type ( s ) for * : 'Infinity' and 'int'
>> > pow ( inf , 0 ) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback ( most recent call last ):
...
TypeError : unsupported operand type ( s ) for ** or pow (): 'Infinity' and 'int'
>> > pow ( 1 , inf ) # doctest: +IGNORE_EXCEPTION_DETAIL
Traceback ( most recent call last ):
...
TypeError : unsupported operand type ( s ) for ** or pow (): 'int' and 'Infinity'
Infinity 객체는 다양한 유형으로 강제 변환될 수 있습니다.
>> > float ( inf ) == float ( 'inf' )
True
>> > float ( - inf ) == float ( '-inf' )
True
>> > str ( inf )
'inf'
>> > str ( - inf )
'-inf'
>> > bool ( inf )
True
>> > bool ( - inf )
True