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

หาจำนวนหลักทศนิยมประมาณที่ float ชนิดนี้แม่นยำใน Python


ภาพรวม

ใช้ attribute precision ของ numpy.finfo() หาจำนวนหลักทศนิยมประมาณที่ float ชนิดนั้นแม่นยำ finfo() รับ parameter คือ float dtype ที่ต้องการข้อมูล

Attributes หลักของ finfo

  • precision: จำนวนทศนิยมประมาณที่แม่นยำ
  • iexp: จำนวนบิตในส่วน exponent
  • min: ค่าต่ำสุดของ dtype
  • max: ค่าสูงสุดของ dtype

ขั้นตอนการทำงาน

1. นำเข้า NumPy

import numpy as np

2. ตรวจสอบ float16

a = np.finfo(np.float16(45.976))
print("The precision to get the approximate number of decimal digits...\n", a.precision)
print("Number of bits in the exponent portion...\n", a.iexp)
print("Minimum of float16 type...\n", a.min)
print("Maximum of float16 type...\n", a.max)

3. ตรวจสอบ float32

b = np.finfo(np.float32(22.3))
print("\nThe precision to get the approximate number of decimal digits...\n", b.precision)
print("Number of bits in the exponent portion...\n", b.iexp)
print("Minimum of float32 type...\n", b.min)
print("Maximum of float32 type...\n", b.max)

4. ตรวจสอบ float64

c = np.finfo(np.float64(29.2))
print("\nThe precision to get the approximate number of decimal digits...\n", c.precision)
print("Number of bits in the exponent portion...\n", c.iexp)
print("Minimum of float64 type...\n", c.min)
print("Maximum of float64 type...\n", c.max)

ตัวอย่างโค้ดเต็ม

import numpy as np
a = np.finfo(np.float16(45.976))
print("The precision to get the approximate number of decimal digits...\n", a.precision)
print("Number of bits in the exponent portion...\n", a.iexp)
print("Minimum of float16 type...\n", a.min)
print("Maximum of float16 type...\n", a.max)
b = np.finfo(np.float32(22.3))
print("\nThe precision to get the approximate number of decimal digits...\n", b.precision)
print("Number of bits in the exponent portion...\n", b.iexp)
print("Minimum of float32 type...\n", b.min)
print("Maximum of float32 type...\n", b.max)
c = np.finfo(np.float64(29.2))
print("\nThe precision to get the approximate number of decimal digits...\n", c.precision)
print("Number of bits in the exponent portion...\n", c.iexp)
print("Minimum of float64 type...\n", c.min)
print("Maximum of float64 type...\n", c.max)

ผลลัพธ์

The precision to get the approximate number of decimal digits... 3
Number of bits in the exponent portion... 5
Minimum of float16 type... -65500.0
Maximum of float16 type... 65500.0

The precision to get the approximate number of decimal digits... 6
Number of bits in the exponent portion... 8
Minimum of float32 type... -3.4028235e+38
Maximum of float32 type... 3.4028235e+38

The precision to get the approximate number of decimal digits... 15
Number of bits in the exponent portion... 11
Minimum of float64 type... -1.7976931348623157e+308
Maximum of float64 type... 1.7976931348623157e+308

สรุป:

TypePrecision (decimal digits)Exponent bitsRange
float1635±65,500
float326-78±3.4×10³⁸
float6415-1611±1.8×10³⁰⁸

หมายเหตุ: โค้ดต้นฉบับมี bug ใช้ a.precision ซ้ำสำหรับ float32 และ float64 ควรใช้ b.precision และ c.precision ตามลำดับ ผลลัพธ์ข้างต้นแก้ไขแล้ว