ในการรับข้อมูลการจำกัดเครื่องสำหรับประเภท float ให้ใช้วิธี numpy.finfo() ใน PythonNumpy พารามิเตอร์แรกคือ float เช่น ชนิดของข้อมูล float ที่ต้องการรับข้อมูล
ขั้นตอน
ขั้นแรก นำเข้าไลบรารีที่จำเป็น -
import numpy as np
ค่าต่ำสุดคือค่าต่ำสุดของ dtype ที่กำหนด และ max คือค่าต่ำสุดของ dtype ที่กำหนด
กำลังตรวจสอบประเภท float16 ด้วยอินสแตนซ์ -
a = np.finfo(np.float16(12.5))
print("Minimum of float16 type...\n",a.min)
print("Maximum of float16 type...\n",a.max) กำลังตรวจสอบประเภท float32 ด้วยอินสแตนซ์ -
b = np.finfo(np.float32(30.5))
print("\nMinimum of float32 type...\n",b.min)
print("Maximum of float32 type...\n",b.max) กำลังตรวจสอบประเภท float ด้วยอินสแตนซ์ -
c = np.finfo(np.float64(55.9))
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 float i.e. the kind of float data type to get information about.
# Checking for float16 type with instances
# The min is the minimum value of given dtype.
# The max is the minimum value of given dtype.
a = np.finfo(np.float16(12.5))
print("Minimum of float16 type...\n",a.min)
print("Maximum of float16 type...\n",a.max)
# Checking for float32 type with instances
b = np.finfo(np.float32(30.5))
print("\nMinimum of float32 type...\n",b.min)
print("Maximum of float32 type...\n",b.max)
# Checking for float type with instances
c = np.finfo(np.float64(55.9))
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