5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at

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

####今回は、無限大を表す「inf」について書いていきます。
I'll write about "inf()",which indicates infinity.

###■ 浮動小数点数 float型のinf
 Floating-point number float-type, "inf"

>>> # コンストラクタ float()の引数に、文字列"inf" を指定
>>> 
>>> f_inf = float("inf")
>>> 
>>> print(f_inf)
inf
>>> 
>>> print(type(f_inf))
<class 'float'>

###■ 負の無限
 Negative "inf"

>>> # infにマイナスをつける
>>> 
>>> f_inf_minus = -float("inf")
>>> 
>>> print(f_inf_minus)
-inf
>>> 
>>> print(type(f_inf_minus))
<class 'float'>

###■ 他の型への変換方法
 How to change to another type

>>> # infは、int型に変換できない
>>> 
>>> print(int(f_inf))
Traceback (most recent call last):
  File "<pyshell#22>", line 1, in <module>
    print(int(f_inf))
OverflowError: cannot convert float infinity to integer
>>> # ただし、文字列str型は、変換可能
>>> 
>>> print(str(f_inf))
inf
>>> print(type(str(f_inf)))
<class 'str'>

###■ infの作成方法
 How to create inf

>>> # ① float( )で作成
>>> 
>>> print(float("inf"))
inf
>>> 
>>> print(float("infinity"))
inf
>>> 
>>> print(float('INF'))
inf
>>> 
>>> print(float('INFinity'))
inf
>>> # ↑小文字・大文字混ざっていても問題なし
>>> # ② sysモジュールによる作成
>>> 
>>> import sys
>>> 
>>> f_inf_num = sys.float_info.max * 2
>>> 
>>> print(f_inf_num)
inf
>>> # ③ mathモジュールによる作成
>>> 
>>> import math
>>> 
>>> print(math.inf)
inf
>>> print(type(math.inf))
<class 'float'>
>>> 
>>> print(float("inf") == math.inf)
True
>>> # ④ Numpyによる作成
>>> 
>>> import numpy as np
>>> 
>>> print(np.inf)
inf
>>> 
>>> print(type(np.inf))
<class 'float'>
>>> 
>>> print(float("inf") == np.inf)
True

###■ infを含んだ演算
 Calculation with "inf"

>>> # 和
>>> 
>>> print(float("inf") + 50)
inf
>>> 
>>> print(float("inf") + float("inf"))
inf
>>> # 差
>>> 
>>> print(float("inf") - 50)
inf
>>> 
>>> print(float("inf") - float("inf"))
nan
>>> # ↑ nanは、非数を表す
>>> 
>>> print(type(float("inf") - float("inf")))
<class 'float'>
>>> # 積
>>> # infに0を掛ける → nan
>>> 
>>> print(float("inf") * 2)
inf
>>> 
>>> print(float("inf") * float("inf"))
inf
>>> print(float("inf") * 0)
nan
>>> # 商
>>> 
>>> # inf を inf で割る → nan
>>> # 0 を inf で割る → 0
>>> # 0 で割る → error
>>> 
>>> print(float("inf") / 2)
inf
>>> 
>>> print(float("inf") / float("inf"))
nan
>>> 
>>> print(float("inf") / 0)
Traceback (most recent call last):
  File "<pyshell#106>", line 1, in <module>
    print(float("inf") / 0)
ZeroDivisionError: float division by zero
>>> # べき乗
>>> # inf の 0乗 → 0
>>> # 1 の inf乗 → 1
>>> # 0 の inf乗 → 0
>>> 
>>> print(float("inf") ** 2)
inf
>>> 
>>> print(float("inf") ** float("inf"))
inf
>>> 
>>> print(float("inf") ** 0)
1.0
>>> 
>>> print(0 ** float("inf"))
0.0
>>> print(2 ** float("inf"))
inf
>>> print(1 ** float("inf"))
1.0

随時に更新していきますので、
定期的な購読をよろしくお願いします。
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

5
3
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
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?