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