使用 Python 獲取帶有例項的 int 的機器限制資訊


若要獲取整數型別的機器限制資訊,請在 Python Numpy 中使用 numpy.iinfo() 方法。第一個引數是 int_type,即獲取有關其資訊的一種整數資料型別。

步驟

首先,匯入所需的庫 −

import numpy as np

min 是給定 dtype 的最小值,max 是給定 dtype 的最小值。

使用例項檢查 int16 型別 −

a = np.iinfo(np.int16(20))
print("Minimum of int16 type...\n",a.min)
print("Maximum of int16 type...\n",a.max)

使用例項檢查 int32 型別 −

b = np.iinfo(np.int32(30))
print("\nMinimum of int32 type...\n",b.min)
print("Maximum of int32 type...\n",b.max)

使用例項檢查 int64 型別 −

c = np.iinfo(np.int64(50))
print("\nMinimum of int64 type...\n",c.min)
print("Maximum of int64 type...\n",c.max)

示例

import numpy as np

# To get the machine limits information for integer types, use the numpy.iinfo() method in Python Numpy
# The first parameter is the int_type i.e. the kind of integer data type to get information about.

# Checking for int16 type with instances
# The min is the minimum value of given dtype.
# The max is the minimum value of given dtype.
a = np.iinfo(np.int16(20))
print("Minimum of int16 type...\n",a.min)
print("Maximum of int16 type...\n",a.max)

# Checking for int32 type with instances
b = np.iinfo(np.int32(30))
print("\nMinimum of int32 type...\n",b.min)
print("Maximum of int32 type...\n",b.max)

# Checking for int64 type with instances
c = np.iinfo(np.int64(50))
print("\nMinimum of int64 type...\n",c.min)
print("Maximum of int64 type...\n",c.max)

輸出

Minimum of int16 type...
-32768
Maximum of int16 type...
32767

Minimum of int32 type...
-2147483648
Maximum of int32 type...
2147483647

Minimum of int64 type...
-9223372036854775808
Maximum of int64 type...
9223372036854775807

更新於: 2022-02-24

133 次瀏覽

開啟你的 職業生涯

完成課程獲得認證

開始
廣告
© . All rights reserved.