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

Kiểm tra xem kiểu float tương tự có các kích thước khác nhau có phải là kiểu con của lớp nổi trong Python hay không

Để kiểm tra xem kiểu float tương tự có các kích thước khác nhau có phải là kiểu con của lớp nổi hay không, hãy sử dụng phương thức thenumpy.issubdtype () trong Python Numpy. Các tham số là loại dtype hoặc đối tượng có thể bị cưỡng chế.

Các bước

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

import numpy as np

Sử dụng phương thức Issubdtype () trong Numpy. Kiểm tra loại dữ liệu dấu chấm động với các kích thước khác nhau -

print("Result...",np.issubdtype(np.float16, np.floating))
print("Result...",np.issubdtype(np.float32, np.floating))
print("Result...",np.issubdtype(np.float64, np.floating))

Ví dụ

import numpy as np

# To test whether similar float type of different sizes are subdtypes of floating class, use the numpy.issubdtype() method in Python Numpy.
# The parameters are the dtype or object coercible to one
print("Using the issubdtype() method in Numpy\n")

# Checking for floating point datatype with different sizes
print("Result...",np.issubdtype(np.float16, np.floating))
print("Result...",np.issubdtype(np.float32, np.floating))
print("Result...",np.issubdtype(np.float64, np.floating))

Đầu ra

Using the issubdtype() method in Numpy

Result... True
Result... True
Result... True