Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ดูขีดจำกัดของชนิดข้อมูล float ด้วย numpy.finfo() ใน Python

เพื่อดูขีดจำกัดของเครื่อง (machine limits) สำหรับชนิดข้อมูล float ให้ใช้ numpy.finfo() ใน NumPy พารามิเตอร์แรกคือชนิด float ที่ต้องการข้อมูล

ขั้นตอนการใช้งาน

import numpy as np

ตรวจสอบค่าต่ำสุดและสูงสุดของ float ต่างๆ

# 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)

# float64 (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

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

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

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