获取 Python 中整数类型的机器限制信息


若要获取整数类型的机器限制信息,请使用 Python Numpy 中的 numpy.iinfo() 方法。第一个参数是 int_type,即要获取信息的整数数据类型。

步骤

首先,导入所需的库 −

import numpy as np

min 是指定数据类型的最小值,max 是指定数据类型的最大值。

检查 int16 类型 −

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

检查 int32 类型 −

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

检查 int64 类型 −

c = np.iinfo(np.int64)
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
# The min is the minimum value of given dtype.
# The max is the minimum value of given dtype.
a = np.iinfo(np.int16)
print("Minimum of int16 type...\n",a.min)
print("Maximum of int16 type...\n",a.max)

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

# Checking for int64 type
c = np.iinfo(np.int64)
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
Result... <class 'numpy.complex128'>

更新时间:2022-02-24

240 浏览

启动你的 职业生涯

完成课程,获得认证

开始
广告
© . All rights reserved.