Computer >> Máy Tính >  >> Lập trình >> Python

Nhận thông tin về giới hạn Máy cho các loại float trong Python

Để lấy thông tin giới hạn máy cho các loại float, hãy sử dụng phương thức numpy.finfo () trong PythonNumpy. Tham số đầu tiên là kiểu float, tức là kiểu dữ liệu float để lấy thông tin.

Các bước

Đầu tiên, hãy nhập thư viện được yêu cầu -

import numpy as np

Giá trị min là giá trị nhỏ nhất của loại đã cho và max là giá trị nhỏ nhất của loại đã cho.

Kiểm tra loại float16 -

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

Kiểm tra kiểu float32 -

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

Kiểm tra loại float64 -

c = np.finfo(np.float64)
print("\nMinimum of float64 type...\n",c.min)
print("Maximum of float64 type...\n",c.max)

Ví dụ

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)

Đầu ra

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