Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Get the approximate number of decimal digits to which this kind of float is precise in Python
To get the approximate number of decimal digits to which a specific float type is precise, use the precision attribute of the numpy.finfo() method in Python NumPy. The finfo() function provides machine limits for floating point types.
Basic Usage
Import NumPy and use finfo() with a float type ?
import numpy as np
# Get float info for different types
info16 = np.finfo(np.float16)
info32 = np.finfo(np.float32)
info64 = np.finfo(np.float64)
print("Float16 precision:", info16.precision)
print("Float32 precision:", info32.precision)
print("Float64 precision:", info64.precision)
Float16 precision: 3 Float32 precision: 6 Float64 precision: 15
Detailed Float Information
The finfo() method provides several useful attributes for understanding float precision ?
import numpy as np
# Checking for float16 type
a = np.finfo(np.float16)
print("Float16 Information:")
print("Precision (decimal digits):", a.precision)
print("Exponent bits:", a.iexp)
print("Minimum value:", a.min)
print("Maximum value:", a.max)
print()
# Checking for float32 type
b = np.finfo(np.float32)
print("Float32 Information:")
print("Precision (decimal digits):", b.precision)
print("Exponent bits:", b.iexp)
print("Minimum value:", b.min)
print("Maximum value:", b.max)
print()
# Checking for float64 type
c = np.finfo(np.float64)
print("Float64 Information:")
print("Precision (decimal digits):", c.precision)
print("Exponent bits:", c.iexp)
print("Minimum value:", c.min)
print("Maximum value:", c.max)
Float16 Information: Precision (decimal digits): 3 Exponent bits: 5 Minimum value: -65500.0 Maximum value: 65500.0 Float32 Information: Precision (decimal digits): 6 Exponent bits: 8 Minimum value: -3.4028235e+38 Maximum value: 3.4028235e+38 Float64 Information: Precision (decimal digits): 15 Exponent bits: 11 Minimum value: -1.7976931348623157e+308 Maximum value: 1.7976931348623157e+308
Key finfo Attributes
| Attribute | Description |
|---|---|
precision |
Approximate number of decimal digits |
iexp |
Number of bits in the exponent |
min |
Minimum representable value |
max |
Maximum representable value |
eps |
Machine epsilon (smallest difference) |
Practical Example with Different Values
You can also pass specific float values to see their type information ?
import numpy as np
# Using specific float values
value16 = np.float16(45.976)
value32 = np.float32(22.3)
value64 = np.float64(29.2)
print("Value:", value16, "- Precision:", np.finfo(value16).precision)
print("Value:", value32, "- Precision:", np.finfo(value32).precision)
print("Value:", value64, "- Precision:", np.finfo(value64).precision)
Value: 45.98 - Precision: 3 Value: 22.299999237060547 - Precision: 6 Value: 29.2 - Precision: 15
Conclusion
Use np.finfo().precision to get the approximate decimal precision of NumPy float types. Float16 has 3 digits, float32 has 6 digits, and float64 has 15 digits of precision.
Advertisements
