No.040 [Python] 무한대 의미의 "inf"
4706 단어 Pythonprogramming
이번에는 무한대를 뜻하는'inf'를 소개해 드리겠습니다.
I'll write about "inf()",which indicates infinity.
■ 부동점 수치flat형 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
Reference
이 문제에 관하여(No.040 [Python] 무한대 의미의 "inf"), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://qiita.com/You-Tarin/items/eebdc39c6f8ffd2770f0텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)