1
1

More than 5 years have passed since last update.

No.041【Python】無限大を表す" inf "について②

Posted at

python-logo-master-v3-TM-flattened.png

前回に引き続き、無限大を表す「inf」について書いていきます。

I'll write about "inf()",which indicates infinity.

■ infによる判定:==/math.isinf()/ np.isinf()

 Judgement by "inf"

>>> # 参考例:float型 最大値を超える・範囲内の値
>>> # eXXX = 10のXXX乗とする
>>> import math
>>> import numpy as np
>>> 
>>> print(1e1000)
inf
>>> 
>>> print(1e100)
1e+100

■ == 演算子

 Calculation with "=="

>>> # 値が無限であるかを判定
>>> 
>>> print(1e1000 == float("inf"))
True
>>> 
>>> print(1e100 == float("inf"))
False
>>> # infの作成方法は複数あり、どれを使っても問題はない
>>> 
>>> print(float("inf") == math.inf == np.inf)
True
>>> 
>>> print(1e1000 == math.inf)
True
>>> 
>>> print(1e1000 == np.inf)
True
>>> 
>>> print(1e100 == math.inf)
False
>>> 
>>> print(1e100 == np.inf)
False
>>> # 以下式でも、判定結果は真(True)となる
>>> 
>>> print(float("inf") == float("inf") * 100)
True

■ mathモジュール:math.isinf( )

 Math module as for inf

>>> # math.isinf()の場合、負の無限に対しても真(True)を返す
>>> 
>>> print(math.isinf(1e1000))
True
>>> 
>>> print(math.isinf(1e100))
False
>>> 
>>> print(math.isinf(-1e1000))
True

■ NumPy:np.isinf( )

 NumPy as for inf

>>> # 上記 mathモジュールと同様、負の無限に対しても真(True)を返す
>>> 
>>> print(np.isinf(1e1000))
True
>>> 
>>> print(np.isinf(1e100))
False
>>> 
>>> print(np.isinf(-1e1000))
True
>>> # 引数にNumPy配列 "ndarray" を指定可能
>>> # 正負のing → "True", その他 → Falseとなる同サイズの"ndarray"を返す
>>> 
>>> a_inf = np.array([1, np.inf, 3, -np.inf])
>>> 
>>> print(a_inf)
[  1.  inf   3. -inf]
>>> 
>>> print(type(a_inf))
<class 'numpy.ndarray'>
>>> 
>>> print(np.isinf(a_inf))
[False  True False  True]
>>> 
>>> 
>>> a_inf[np.isinf(a_inf)] = 0
>>> 
>>> print(a_inf)
[1. 0. 3. 0.]

■ infの比較

 Infinite comparison

>>> # 比較演算子(>, <)でinfの比較が可能
>>> 
>>> # float および inf の値と比較が可能
>>> # ただし、nanの比較は Falseとなる

■ floatとの比較

 Comparison with "float"

>>> import sys
>>> 
>>> print(sys.float_info.max)
1.7976931348623157e+308
>>> 
>>> print(float("inf") > sys.float_info.max)
True
>>> 
>>> print(-float("inf") < sys.float_info.max)
True

■ nanとの比較

 Comparison with "nan"

>>> # 前述の通り、 nanとの比較は全て"False"となる
>>> 
>>> print(float("inf") > float("nan"))
False
>>> 
>>> print(float("inf") < float("nan"))
False
>>> 
>>> print(float("inf") == float("nan"))
False

■ 整数との比較

 Calculation with "integer"

>>> print(float("inf") > 100)
True
>>> # Python3では、integerの上限はない
>>> # ただし、infはそれよりも値が大きい
>>> 
>>> large_int = int(sys.float_info.max) * 100
>>> 
>>> print(large_int)
17976931348623157081452742373170435679807056752584499659891747680315726078002853876058955863276687817154045895351438246423432132688946418276846754670353751698604991057655128207624549009038932894407586850845513394230458323690322294816580855933212334827479782620414472316873817718091929988125040402618412485836800
>>> 
>>> print(type(large_int))
<class 'int'>
>>> 
>>> print(large_int > sys.float_info.max)
True
>>> 
>>> print(float("inf") > large_int)
True
>>> # float最大値以下のintの値は、float()でfloatに変換可能
>>> # ただし、floatの最大値を超える場合なNG
>>> 
>>> print(float(10**306))
1e+306
>>> 
>>> print(float(10**309))
Traceback (most recent call last):
  File "<pyshell#156>", line 1, in <module>
    print(float(10**309))
OverflowError: int too large to convert to float

随時に更新していきますので、
定期的な購読をよろしくお願いします。
I'll update my article at all times.
So, please subscribe my articles from now on.

本記事について、
何か要望等ありましたら、気軽にメッセージをください!
If you have some requests, please leave some messages! by You-Tarin

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1