获取 Python 中浮点类型机器限制信息


要获取浮点类型机器限制信息,请在 Python Numpy 中使用 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

更新于: 2022-02-24

543 次浏览

开启你的 职业生涯

完成课程即可获得认证

开始
广告
© . All rights reserved.