Pythonでfloatタイプのマシン制限情報を取得する
floatタイプのマシン制限情報を取得するには、PythonNumpyのnumpy.finfo()メソッドを使用します。最初のパラメータはフローティングタイプです。つまり、情報を取得するためのフロートデータ型の種類です。
ステップ
まず、必要なライブラリをインポートします-
import numpy as np
minは指定されたdtypeの最小値であり、maxは指定されたdtypeの最小値です。
float16タイプの確認-
a = np.finfo(np.float16)
print("Minimum of float16 type...\n",a.min)
print("Maximum of float16 type...\n",a.max) float32タイプの確認-
b = np.finfo(np.float32)
print("\nMinimum of float32 type...\n",b.min)
print("Maximum of float32 type...\n",b.max) float64タイプの確認-
c = np.finfo(np.float64)
print("\nMinimum of float64 type...\n",c.min)
print("Maximum of float64 type...\n",c.max) 例
import numpy as np
# To get the machine limits information for float types, use the numpy.finfo() method in Python Numpy
# The first parameter is the floating type i.e. the kind of float data type to get information about.
# Checking for float16 type
# The min is the minimum value of given dtype.
# The max is the minimum value of given dtype.
a = np.finfo(np.float16)
print("Minimum of float16 type...\n",a.min)
print("Maximum of float16 type...\n",a.max)
# Checking for float32 type
b = np.finfo(np.float32)
print("\nMinimum of float32 type...\n",b.min)
print("Maximum of float32 type...\n",b.max)
# Checking for float64 type
c = np.finfo(np.float64)
print("\nMinimum of float64 type...\n",c.min)
print("Maximum of float64 type...\n",c.max) 出力
Minimum of float16 type... -65500.0 Maximum of float16 type... 65500.0 Minimum of float32 type... -3.4028235e+38 Maximum of float32 type... 3.4028235e+38 Minimum of float64 type... -1.7976931348623157e+308 Maximum of float64 type... 1.7976931348623157e+308
-
Pythonでタイプをチェックするための標準的な方法は何ですか?
オブジェクト、xが正確に指定されたタイプ(サブタイプではない)のインスタンスであるかどうかを確認する場合は、typeを使用してそのタイプを取得し、isステートメントを使用して確認できます。 例 x = "Hello" if type(x) is str: print("x is an instance of str") 出力 これにより出力が得られます x is an instance of str xがMyClassのインスタンスであるか、MyClassのサブクラスであるかを確認する場合は、isinstanceメソッド呼び出
-
先週の水曜日のPython日付オブジェクトを取得するにはどうすればよいですか?
Pythonの日付計算を使用して、先週の水曜日のPythonの日付オブジェクトを取得できます。今日の曜日が何であれ、それから2を引き、結果のモジュラスを7で取ると、水曜日がどのように戻ったかがわかります。 例 from datetime import date from datetime import timedelta today = date.today() offset = (today.weekday() - 2) % 7 last_wednesday = today - timedelta(days=offset) 出力 これにより、出力が得られます- 2017-12-27